【发布时间】:2017-12-27 03:18:21
【问题描述】:
我现在尝试在 CFB 模式下使用三重 DES 算法使用给定的 3 个密钥和初始化向量 (iv) 加密一些纯文本。我在 python 中使用 pycrypto 的实现如下。
import base64
from Crypto.Cipher import DES3
key1 = b'key1____'
key2 = b'key2____'
key3 = b'key3____'
key = key1 + key2 + key3
initialization_vector = b'init____'
des3 = DES3.new(key, mode=DES3.MODE_CFB, IV=initialization_vector)
plain_text = "this is plain text."
encrpted = des3.encrypt(plain_text)
b64 = base64.b64encode(encrpted)
print('key = {}'.format(key.hex()))
print('iv = {}'.format(initialization_vector.hex()))
print('encrypted = {}'.format(b64.decode()))
这个程序输出:
key = 6b6579315f5f5f5f6b6579325f5f5f5f6b6579335f5f5f5f
iv = 696e69745f5f5f5f
encrypted = TGlbmL795TWPX0h39F19N6WZ6Q==
为了交叉检查结果,我比较了 python 的输出和openssl 命令的输出。但是openssl 输出不同的结果。
$ echo -n "this is plain text." | openssl des-ede3-cfb -K 6b6579315f5f5f5f6b6579325f5f5f5f6b6579335f5f5f5f -iv 696e69745f5f5f5f -base64
TEkV+qFiNHi+C8cxpG2qyzGw9A==
为什么即使算法和模式相同,输出也会有所不同?非常感谢任何帮助。
【问题讨论】:
标签: python encryption openssl pycrypto