如何使用blowfish在python和php之间发送加密消息进行加密


Howto send encrypted messages between python and php using blowfish for encryption?

我想使用blowfish用已知密码在php中加密消息。然后我想用python解密这个消息。

即使您想用一种语言加密而在其他地方解密,这也很有用。

我进行了相当广泛的搜索,但没有找到任何结论性的解决方案,所以我想把我的发现记录下来。

请注意,使用相同的语言(如python或php)加密/解密非常简单。

这个解决方案非常简单,但我花了一段时间才弄清楚。

河豚参数

  • 密码长度应为16
  • 使用模式mode_ECB
  • 加密长度的数据应始终可被16个空格或任何其他字符整除。我在下面的例子中取了一个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中的一些字符串。首先进行b64解码,然后解密结果。

Remember to pad your data such that the len is divisible by 16. 

加密和解密快乐!!!