当用户提供密钥和密文时,我想将密文解密为普通文本
这是我的密码:
from Crypto.Cipher import DES
key = input('Enter your key: ').encode('utf-8')
myDes = DES.new(key, DES.MODE_ECB)
print('Please select option:\n1. Encryption\n2. Decryption\n3. Exit')
while True:
user_choice = input("Choose a option: ")
if user_choice == "1":
plain_text = input("Please enter your text: ")
modified_plain_text = plain_text.encode("utf-8")
cipher_text = myDes.encrypt(plain_text.encode("utf-8"))
print(f"Encrypted text: {cipher_text}")
elif user_choice == "2":
user_cipher_text = input(
"Please enter your cipher text: ").encode('utf-8')
text = myDes.decrypt(user_cipher_text, DES.block_size)
elif user_choice == "3":
print("Quitting The Program....")
break
else:
print("Please Choose a correct option")
但当我运行它时,我得到一个错误:
Traceback (most recent call last):
File "C:\Users\Manish\Downloads\DES.py", line 17, in <module>
text = myDes.decrypt(user_cipher_text,DES.block_size)
File "C:\python38\lib\site-packages\Crypto\Cipher\_mode_ecb.py", line 183, in decrypt
raise TypeError("output must be a bytearray or a writeable memoryview")
TypeError: output must be a bytearray or a writeable memoryview
decrypt
的第二个参数不是(任何内容的)长度。这是一个可选参数,如果提供,将导致decrypt尝试将输出放在那里。因此,该参数必须是可写的bytearray
或memoryview
只需去掉最后一个参数并使其
尝试修改程序以生成十六进制编码的密文输出,并接受十六进制编码的密文输入。这样,您可以将密文打印为常规文本而不是字节数组,用户可以使用键盘以常规字符串(而不是字节数组)的形式输入密文。(您可以使用不同的编码方法,例如base64而不是十六进制)。我还清理了其他一些东西:
请注意,键和输入必须是8字节的倍数。如果是,则该功能将按预期运行:
相关问题 更多 >
编程相关推荐