解决方案:TypeError:“ NoneType”类型的对象没有len()

时间:2019-11-29 14:46:37

标签: python aes

所以,我对pyaes库有点困惑,我只想加密和解密一个简单的字符串,所以我创建了这两个函数:

def aes_encrypt(key, plaintext):
   aes = pyaes.AESModeOfOperationCTR(key)
   encrypted_text = aes.encrypt(plaintext)
   print(encrypted_text)

def aes_decrypt(key, encrypted_text):
   aes = pyaes.AESModeOfOperationCTR(key)
   decrypted_text = aes.decrypt(encrypted_text)
   print(decrypted_text)

使用key_265 = os.urandom(32)生成密钥 我尝试执行以下几行:

encrypted_text = aes_encrypt(key_256, "Hi World!")
decrypted_text = aes_decrypt(key_256, encrypted_text)

但是我收到此错误:while len(self._remaining_counter) < len(plaintext): TypeError: object of type 'NoneType' has no len()

有人可以解释我为什么会发生这种情况,并告诉我可能的解决方案?

这可能是伪造的帖子,但我还没有在其他类似线程上找到解决方案。

1 个答案:

答案 0 :(得分:0)

我坐在这里,看着pyaes的实现,以为“但它应该起作用” ...

问题出在你的功能上。

encrypted_text = aes_encrypt(key_256, "Hi World!")

encrypted_text的值是什么?让我们看一下功能:

def aes_encrypt(key, plaintext):
   aes = pyaes.AESModeOfOperationCTR(key)
   encrypted_text = aes.encrypt(plaintext)
   print(encrypted_text)

没有return。打印与返回不同。因此,该函数隐式返回None


修复:在return encrypted_text之后添加print。 +解密也一样。