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

2024-06-11 20:46:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这个小python程序应该使用128位密钥在CFB模式下使用AES将plain加密到cipher

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))

我从NIST test vectors(CFB128VarTxt128.rsp)中取了这个密钥、IV和普通密码的组合。对于这个特殊的组合,我希望密码是:

^{pr2}$

但是pycrypto计算

3a 81 e1 d4 b8 24 75 61 46 31 63 4b 5c 79 d6 bc

第一个字节是正确的,而其他字节不匹配。我也尝试了不同的测试向量,但结果保持不变。除第一个字节外,所有字节都不匹配。在

我非常肯定,NIST测试向量是有效的,因为我以前在使用AES和Crypto++时使用了它们,而且我也非常确定pycrypto的实现是正确的,因为它的输出与在线工具(如this page)一致。很明显,是我用错了工具。。。在

有人知道如何用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

Tags: keyfor字节密钥crypto向量aesnist
2条回答

我也得到了和你一样的结果AES模式,但当我使用AES.MODE U 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)

输出

^{pr2}$

您的^{}调用中缺少一个关键字参数segment_size。这是反馈大小,默认为8。如果您的代码行更改为

aes = AES.new(key, AES.MODE_CFB, iv, segment_size=128)

你得到了正确的结果。在

如文件所述:

segment_size (integer) - (Only MODE_CFB).The number of bits the plaintext and ciphertext are segmented in. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.

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

相关问题 更多 >