我想使用blowfish 用已知密码加密 php 中的消息。然后我想在 python 中解密此消息。
即使您想用一种语言加密并在其他语言中解密,这也很有用。
我进行了广泛的搜索,但找不到任何结论性的解决方案,所以我想记录一下我的发现。
请注意,使用相同的语言(例如 python 或 php)加密/解密非常简单。
最佳答案
这个解决方案非常简单,但我花了一段时间才弄清楚。
河豚参数
- 密码长度应为 16
- 使用模式MODE_ECB。
- 加密的数据长度应始终能被空格或任何其他字符的 16 pad 整除。我在下面的示例中采用了 16 长度的数据字符串。
PHP 代码:
<?php
$passw='secretPassword12';
$ntext='helloWorld123456';
$enc = base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $passw, $ntext, MCRYPT_MODE_ECB));
echo '<div>'.$enc.'</div';
输出 3C8f2kaD8Of0INYk3l9qEg== python 代码:
from Crypto.Cipher import Blowfish
from base64 import b64encode, b64decode
passw='secretPassword12'
ntext='helloworld123456'
cipher=Blowfish.new(passw, Blowfish.MODE_ECB)
encStr=b64encode(cipher.encrypt(data))
print encStr
此代码也输出 3C8f2kaD8Of0INYk3l9qEg==
现在假设你想解密在 php 中加密的 python 中的某个字符串。首先进行b64decode,然后解密结果。
Remember to pad your data such that the len is divisible by 16.
加密和解密快乐!!!
关于php - 如何使用blowfish进行加密在python和php之间发送加密消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12046419/