Python 如何使用DES3解密密文?

Python 如何使用DES3解密密文?,python,cryptography,des,Python,Cryptography,Des,所以当我运行代码时 它成功解密,但当我选择2而不是加密时,我得到一个错误: from Crypto.Util.Padding import pad, unpad from Crypto.Cipher import DES3 import binascii from Crypto.Random import get_random_bytes #iv = b"23456789" iv = get_random_bytes(8) #key = input('Please ente

所以当我运行代码时 它成功解密,但当我选择2而不是加密时,我得到一个错误:

from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import DES3
import binascii
from Crypto.Random import get_random_bytes

#iv = b"23456789"
iv = get_random_bytes(8)
#key = input('Please enter a 8 byte key: ').encode('utf-8')

#new_key = pad(DES3.adjust_key_parity(key),24)
print('Please select option:\n1. Encryption\n2. Decryption\n3. Exit')


while True:
    user_choice = input("Choose a option: ")
    if user_choice == "1":
        data = input('Enter the text: ').encode('utf-8')  # 9 bytes
        new_key = DES3.adjust_key_parity(get_random_bytes(24))
        
        cipher1 = DES3.new(new_key, DES3.MODE_CFB,iv)
        ct = cipher1.encrypt(data).hex()
        prt_key = new_key.hex()
        print(new_key)
        print(binascii.unhexlify(prt_key))
        print(f'The encryption key is: {prt_key}')
        print(f'The cipher text is: {ct}')

    elif user_choice == "2":
        get_key = input('Enter your encryption key: ')
        get_hex_key = binascii.unhexlify(get_key)
        print(get_hex_key)
        hex_value = input('Enter the cipher text: ')
        cip_txt = binascii.unhexlify(hex_value) # #cip_txt = bytes.fromhex(f'{hex_value}')
        cipher2 = DES3.new(get_hex_key, DES3.MODE_CFB, iv)
        pt = cipher2.decrypt(cip_txt)
        print(f"The decrypted text is: {pt.decode('utf-8')}")


    elif user_choice == "3":
        print("Quitting The Program....")
        break

    else:
        print("Please Choose a correct option")
错误:

Please select option:
    1. Encryption
    2. Decryption
    3. Exit
    Choose a option: 1
    Enter the text: hey
    b',\x85\x0b\x01n/p^\xf4@[\x10Fb\xce\xb67[\x08\xec\x97O\x97\xbc'
    b',\x85\x0b\x01n/p^\xf4@[\x10Fb\xce\xb67[\x08\xec\x97O\x97\xbc'
    The encryption key is: 2c850b016e2f705ef4405b104662ceb6375b08ec974f97bc
    The cipher text is: a44c2c
    Choose a option: 2
    Enter your encryption key: 2c850b016e2f705ef4405b104662ceb6375b08ec974f97bc
    b',\x85\x0b\x01n/p^\xf4@[\x10Fb\xce\xb67[\x08\xec\x97O\x97\xbc'
    Enter the cipher text: a44c2c
    The decrypted text is: hey
请选择选项:
1.加密
2.解密
3.出口
选择一个选项:2
输入加密密钥:1337518a8f3dd5458a01d502a780c416166e025468ae7929
b'\x137Q\x8a\x8f=\xd5E\x8a\x01\xd5\x02\xa7\x80\xc4\x16\x16n\x02Th\xaey)'
输入密码文本:54032077
回溯(最近一次呼叫最后一次):
文件“C:\Users\Manish\Downloads\3DES\u enc&dec\u CFB.py”,第36行,在
打印(f“解密文本为:{pt.decode('utf-8')}”)
UnicodeDecodeError:“utf-8”编解码器无法解码位置3中的字节0x8c:无效的开始字节

我正在尝试创建一个既能加密又能解密密文的程序,因此,当我首先运行代码时,它会生成密文,它也能解密密文,但当我再次运行代码并选择选项2时,它无法解密文本。

如果您解密随机字节或使用错误的密钥,那么您将获得随机字节返回。如果不希望发生这种情况,则需要经过身份验证的密码或密文上的MAC,并在解密之前验证

当您尝试使用UTF-8对随机字节进行解码时,UTF-8编码很可能无效,当遇到具有无效值的字节时,UTF-8编码将停止解码。

请参阅
Please select option:
1. Encryption
2. Decryption
3. Exit
Choose a option: 2
Enter your encryption key: 1337518a8f3dd5458a01d502a780c416166e025468ae7929
b'\x137Q\x8a\x8f=\xd5E\x8a\x01\xd5\x02\xa7\x80\xc4\x16\x16n\x02Th\xaey)'
Enter the cipher text: 54032077
Traceback (most recent call last):
  File "C:\Users\Manish\Downloads\3DES_enc&dec_CFB.py", line 36, in <module>
    print(f"The decrypted text is: {pt.decode('utf-8')}")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 3: invalid start byte