Can't use 256-bits counter in AES.MODE_CTR cipher in python

眉间皱痕 提交于 2019-12-08 08:55:47

问题


guys. I use crypto library for AES encryption and decryption. AES-128 works perfect:

ctr = Counter.new(128)
key = os.urandom(16)
obj = AES.new(key, AES.MODE_CTR, counter=ctr)

But when I change 128 to 256, and 16 to 32 error occurs:
ValueError: unsupported format character 'b' (0x62) at index 29
Could you help me, any suggestions?


回答1:


The counter size is the same as the block size of the block cipher. The block size and key size of a cipher are not directly related.

Now AES-256 has a key size of 256 bit and a block size of 128 bits. Rijndael, of which AES is a subset, can have a block size of 256 bit. But AES, using a key size of 128, 192 or 256 bits, will still have a block size of precisely 128 bits. And that's just because it has been defined that way.

So basically you should always leave ctr to Counter.new(128). If you want to use AES-256, increase your key to 32 bytes using os.urandom(32).


If you want to have hints on how to use / format the counter, please take a look at NIST SP 800-38A, Appendix B.



来源:https://stackoverflow.com/questions/43988367/cant-use-256-bits-counter-in-aes-mode-ctr-cipher-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!