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

问题描述 投票:2回答:1

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

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

我搜索得相当广泛,但找不到任何确凿的解决方案,所以我想记录我的发现。

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

php python django blowfish encryption-symmetric
1个回答
2
投票

这个解决方案非常简单,但我需要一段时间才能弄明白。

河豚Params

  • 密码长度应为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中的某些字符串。首先执行b64decode然后解密结果。

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

快乐的加密和解密!

© www.soinside.com 2019 - 2024. All rights reserved.