Python pycrypto不复制AES的NIST测试向量(CFB模式)

Python pycrypto不复制AES的NIST测试向量(CFB模式),python,encryption,aes,pycrypto,Python,Encryption,Aes,Pycrypto,这个小型python程序应该使用128位密钥在CFB模式下使用AES对普通密码进行加密 from Crypto.Cipher import AES # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 key = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' iv = b'\x80\x00\x00\x

这个小型python程序应该使用128位密钥在CFB模式下使用AES对普通密码进行加密

from Crypto.Cipher import AES

#            1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
key   = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
iv    = b'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
plain = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

aes = AES.new(key, AES.MODE_CFB, iv)
cipher = aes.encrypt(plain)

print(' '.join('{:2x}'.format(b) for b in cipher))
我从其中一个(CFB128VarTxt128.rsp)中获取了这个密钥、IV和普通密码组合。对于这种特殊的组合,我希望密码:

3a d7 8e 72 6c 1e c0 2b 7e bf e9 2b 23 d9 ec 34
但是pycrypto计算

3a 81 e1 d4 b8 24 75 61 46 31 63 4b 5c 79 d6 bc
第一个字节正确,而其他字节不匹配。我还尝试了不同的测试向量,但结果保持不变。除第一个字节外,所有字节都不匹配

我非常确信,NIST测试向量是有效的,因为我以前在使用AES和Crypto++时使用过它们,而且我也非常确信,pycrypto的实现是正确的,因为它的输出与在线工具(例如)一致。显然,是我,以错误的方式使用这些工具

有人知道如何用pycrypto复制NIST测试向量吗

这就是NIST的例子

# CAVS 11.1
# Config info for aes_values
# AESVS VarTxt test data for CFB128
# State : Encrypt and Decrypt
# Key Length : 128
# Generated on Fri Apr 22 15:11:53 2011
...
COUNT = 0
KEY = 00000000000000000000000000000000
IV = 80000000000000000000000000000000
PLAINTEXT = 00000000000000000000000000000000
CIPHERTEXT = 3ad78e726c1ec02b7ebfe92b23d9ec34

在使用AES.MODE_CFB时,我也会得到与您相同的结果,但在使用AES.MODE_CBC时,我会得到您期望的结果

from Crypto.Cipher import AES

def show(b):
    print(*['{:02x}'.format(u) for u in b])

key   = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
iv    = b'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
plain = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

crypto = AES.new(key, AES.MODE_CBC, iv)
cipher = crypto.encrypt(plain)
show(cipher)

# We need a fresh AES object to decrypt
crypto = AES.new(key, AES.MODE_CBC, iv)
decoded = crypto.decrypt(cipher)
show(decoded)
输出

3a d7 8e 72 6c 1e c0 2b 7e bf e9 2b 23 d9 ec 34
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

调用中缺少关键字参数
segment\u size
。这是反馈大小,默认为8。如果您的代码行更改为

aes = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
你得到了正确的结果

如文件所述:

段大小(整数)-(仅模式\u CFB)。数据段的位数 明文和密文分为两部分。它必须是8的倍数。 如果未指定0,则假定为8


您的结果与NIST文档中可能标记为“CFB8”的内容相对应。

我得到的结果与您使用PyCrypto 2.6.0时得到的结果相同……啊,这很有道理!RTFM的thx。现在一切正常。谢谢