无法在AES中使用256位计数器.python中的MODE_CTR密码 [英] Can't use 256-bits counter in AES.MODE_CTR cipher in python

查看:189
本文介绍了无法在AES中使用256位计数器.python中的MODE_CTR密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。我使用 crypto 库进行AES加密和解密。 AES-128完美运行:

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

但是当我将128更改为256,并且将16更改为32时发生错误:

ValueError:索引29处不受支持的格式字符'b'(0x62)

您能帮我什么建议吗?

解决方案

计数器的大小与计数器的块大小相同分组密码。密码的块大小和密钥大小没有直接关系。



现在,AES-256的密钥大小为256位,而 128位的块大小。 Rijndael是AES的一个子集,可以具有256位的块大小。但是使用128、192或256位密钥大小的AES仍将具有恰好128位的块大小。



因此,基本上,您应该始终将 ctr 保留为 Counter.new(128)。如果要使用AES-256,请使用 os.urandom(32)密钥增加到32个字节。 / p>




如果您想获得有关如何使用/格式化计数器的提示,请查看 NIST SP 800-38A,附录B


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?

解决方案

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.

这篇关于无法在AES中使用256位计数器.python中的MODE_CTR密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆