【问题标题】:Different outputs from openssl command and pycrypto when encrypting with Triple DES in CFB mode在 CFB 模式下使用 Triple DES 加密时,openssl 命令和 pycrypto 的输出不同
【发布时间】: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


    【解决方案1】:

    我自己解决了这个问题。不同之处在于 CFB 的默认段大小。 pycrypto 的默认是 8 位,而 openssl 的默认是 64 位。通过在openssl 命令中指定段大小,我得到了相同的结果,如下所示。

    ~$ echo -n "this is plain text." | openssl des-ede3-cfb8 -K 6b6579315f5f5f5f6b6579325f5f5f5f6b6579335f5f5f5f -iv 696e69745f5f5f5f -base64
    TGlbmL795TWPX0h39F19N6WZ6Q==
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多