在 CFB 模式下使用三重 DES 加密时 openssl 命令和 pycrypto 的不同输出
Different outputs from openssl command and pycrypto when encrypting with Triple DES in CFB mode
我现在正尝试在 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==
为什么算法和模式一样,输出却不一样?非常感谢任何帮助。
我已经自己解决了这个问题。不同之处在于 CFB 的默认段大小。 pycrypto 的默认是 8 位,而 openssl 的默认是 64 位。通过在 openssl
命令中指定段大小,我得到了相同的结果,如下所示。
~$ echo -n "this is plain text." | openssl des-ede3-cfb8 -K 6b6579315f5f5f5f6b6579325f5f5f5f6b6579335f5f5f5f -iv 696e69745f5f5f5f -base64
TGlbmL795TWPX0h39F19N6WZ6Q==
我现在正尝试在 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==
为什么算法和模式一样,输出却不一样?非常感谢任何帮助。
我已经自己解决了这个问题。不同之处在于 CFB 的默认段大小。 pycrypto 的默认是 8 位,而 openssl 的默认是 64 位。通过在 openssl
命令中指定段大小,我得到了相同的结果,如下所示。
~$ echo -n "this is plain text." | openssl des-ede3-cfb8 -K 6b6579315f5f5f5f6b6579325f5f5f5f6b6579335f5f5f5f -iv 696e69745f5f5f5f -base64
TGlbmL795TWPX0h39F19N6WZ6Q==