1

我在 Python 3 上使用 pycryptodome 进行 AES-CTR 加密时遇到了困难。数据可能约为 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
4

1 回答 1

1

我想到了。Nonce 仅比块大小少 1 个字节,因此计数器模式只能产生 256 个块,这将允许加密 4096 个字节。如果 nonce 短几个字节,问题就会消失。

于 2017-04-18T01:30:30.887 回答