pycryptodome: OverflowError: The counter has wrapped around in CTR mode
pycryptodome: OverflowError: The counter has wrapped around in CTR mode
我在 Python 上使用 pycryptodome 进行 AES-CTR 加密时遇到困难 3. 数据可能约为 1000 字节,但当它足够长时就会中断。我不明白这个错误应该意味着什么或如何解决它。
from os import urandom
from Crypto.Cipher import AES
cipher = AES.new(urandom(16), AES.MODE_CTR, nonce=urandom(15))
cipher.encrypt(urandom(10000))
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-116-a48990362615> in <module>()
3
4 cipher = AES.new(urandom(16), AES.MODE_CTR, nonce=urandom(15))
----> 5 cipher.encrypt(urandom(10000))
6
/usr/local/lib/python3.5/dist-packages/Crypto/Cipher/_mode_ctr.py in encrypt(self, plaintext)
188 if result:
189 if result == 0x60002:
--> 190 raise OverflowError("The counter has wrapped around in"
191 " CTR mode")
192 raise ValueError("Error %X while encrypting in CTR mode" % result)
OverflowError: The counter has wrapped around in CTR mode
我想通了。 Nonce 仅比块大小少 1 个字节,因此计数器模式只能生成 256 个块,这将允许加密 4096 个字节。如果 nonce 短几个字节,问题就会消失。
我在 Python 上使用 pycryptodome 进行 AES-CTR 加密时遇到困难 3. 数据可能约为 1000 字节,但当它足够长时就会中断。我不明白这个错误应该意味着什么或如何解决它。
from os import urandom
from Crypto.Cipher import AES
cipher = AES.new(urandom(16), AES.MODE_CTR, nonce=urandom(15))
cipher.encrypt(urandom(10000))
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-116-a48990362615> in <module>()
3
4 cipher = AES.new(urandom(16), AES.MODE_CTR, nonce=urandom(15))
----> 5 cipher.encrypt(urandom(10000))
6
/usr/local/lib/python3.5/dist-packages/Crypto/Cipher/_mode_ctr.py in encrypt(self, plaintext)
188 if result:
189 if result == 0x60002:
--> 190 raise OverflowError("The counter has wrapped around in"
191 " CTR mode")
192 raise ValueError("Error %X while encrypting in CTR mode" % result)
OverflowError: The counter has wrapped around in CTR mode
我想通了。 Nonce 仅比块大小少 1 个字节,因此计数器模式只能生成 256 个块,这将允许加密 4096 个字节。如果 nonce 短几个字节,问题就会消失。