AES decrytion API将不接受str参数

问题描述

这些是解密功能。​​

def decrypt(info,key):
    msg=info
    PAD="%"
    decipher=AES.new(new_pwd,AES.MODE_ECB)
    pt=decipher.decrypt(msg).decode('utf-8')  # ERROR OCCURS HERE
    pad_index=pt.find(PAD)
    result=pt[:pad_index]
    return result

def decrypt_file(filename,new_pwd):
    with open(filename,'r') as f:
        ciphertext=f.read()
    dec=decrypt(ciphertext,new_pwd)
    with open(filename[:-4],'w') as f:
         f.write(dec)

这是我在运行代码时遇到的错误

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py",line 1883,in __call__
    return self.func(*args)
  File "first.py",line 85,in decrypt_text_file
    decrypt_file(fname,new_pwd)
  File "first.py",line 63,in decrypt_file
    dec=decrypt(ciphertext,line 29,in decrypt
    pt=decipher.decrypt(msg).decode('utf-8')
  File "C:\Users\aravi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\python38\site-packages\Crypto\Cipher\_mode_ecb.py",line 190,in decrypt
    c_uint8_ptr(ciphertext),File "C:\Users\aravi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\python38\site-packages\Crypto\Util\_raw_api.py",line 243,in c_uint8_ptr
    raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <class 'str'> cannot be passed to C code

我不知道要解决这个问题。

解决方法

.decrypt() 需要字节而不是字符串。

将错误行改为:

pt=decipher.decrypt(msg.encode()).decode('utf-8')