为什么所有对AES的输入都必须是16的倍数? [英] Why must all inputs to AES be multiples of 16?
问题描述
我正在使用AES的PyCrypto实现,并且尝试使用24字节密钥对某些文本(24字节)进行加密。
I'm using the PyCrypto implementation of AES and I'm trying to encrypt some text (24 bytes) using a 24 byte key.
aes_ecb = AES.new('\x00'*24, AES.MODE_ECB)
aes_ecb.encrypt("123456"*4)
我收到此令人惊讶的错误 ValueError:输入字符串长度必须是16的倍数。
那么为什么我的输入必须是16的倍数?对我来说,输入字符串的长度必须是密钥大小的倍数,这对我来说更有意义,因为这将允许密钥和明文块之间进行很好的按位运算。
I get this surprising error ValueError: Input strings must be a multiple of 16 in length
So why is it that my input must be a multiple of 16? It would make more sense to me that the input string length must be a multiple of my key size, because this would allow nice bitwise operations between the key and blocks of plaintext.
推荐答案
AES是分组密码。引自Wikipedia页面:分组密码是在固定长度的位组上运行的确定性算法。
AES is a block cipher. Quote from the Wikipedia page: "a block cipher is a deterministic algorithm operating on fixed-length groups of bits".
AES只能使用128位的块(如您所注意到的那样,为16个字符)。
AES can only work with blocks of 128 bits (that is, 16 chars, as you noticed).
如果输入的长度可以不是128的倍数,具体取决于您的应用程序,则可能必须非常小心处理填充。
If your input can have lengths others than a multiple of 128, depending on your application, you may have to be extremely careful how you handle padding.
这篇关于为什么所有对AES的输入都必须是16的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!