【发布时间】:2019-03-26 09:24:57
【问题描述】:
您好,我正在尝试使用 pycrypto 执行 BlowFish 加密/解密
这是我的示例代码文件,在解密数据时加密效果很好
它只是打印:
Hello 8g
而不是this
这里是 BlowFish 加密 和 解密 的完整示例代码,不知道我需要添加什么填充,我知道 BlowFISH 有一个固定数据块大小为 8 字节,其密钥长度可以从 32 到 448 位(4 到 56 字节)不等。
from Crypto.Cipher import Blowfish
from Crypto import Random
from struct import pack
bs = Blowfish.block_size
import os
encryptedpass = "myverystrongpassword"
plaintextMessage = "Hello 8gwifi.org"
iv = os.urandom(Blowfish.block_size)
bs = Blowfish.block_size
# ENcryption
cipher = Blowfish.new(encryptedpass, Blowfish.MODE_CBC, iv)
plen = bs - divmod(len(plaintextMessage),bs)[1]
padding = [plen]*plen
padding = pack('b'*plen, *padding)
ct = iv + cipher.encrypt(plaintextMessage + padding)
#Decryption
cipher = Blowfish.new(encryptedpass, Blowfish.MODE_CBC, iv)
msg = cipher.decrypt(ct[bs:])
print msg
【问题讨论】:
标签: python encryption pycrypto