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

标签 php python django blowfish encryption-symmetric

我想使用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/

相关文章:

php - CakePHP 编码指南 : Why are some properties camelCased instead of CamelCased?

php - 碎片化的网址导致 SEO 出现问题

python - 如何合并两个表并将行转置为列

python - 按时间戳和列组合 Pandas DataFrame 行

python - Python 中不区分大小写的集合比较

PHP 守护进程 - 从浏览器接收命令?

php - 是我的 php 解释器疯了,还是我就是……?

python - Pandas:使用 groupby 获取每个数据类别的平均值

django - 如何使 Django View 仅可供未经身份验证的用户访问?

django - Django模型中的非数据库字段