2 回答
TA贡献1780条经验 获得超4个赞
我使用了@Ralf 和@Kevin_Fontaine 的建议来修复您的代码,使其正常工作。这是更正后的脚本,更改了几行并标有注释:
import base64 # new line here
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
backend = default_backend()
def operation_getmessage():
padder = padding.PKCS7(128).padder() # new line here
messagesinput = input("Please, insert messages to decrypt: ")
messagesinput = bytes(messagesinput, "utf-8")
message = padder.update(messagesinput)
message += padder.finalize()
return message
keyinput = input("Please insert the key used for encrypt : ")
print(keyinput)
key = bytes(str(keyinput), 'ascii')
print("key value is :", key)
message = operation_getmessage()
cipher = Cipher(algorithms.TripleDES(key), modes.ECB(), backend=backend)
encryptor = cipher.encryptor()
message_encrypt = encryptor.update(message) + encryptor.finalize()
print("Value of message_encrypted:", base64.b64encode(message_encrypt)) # new line here
test = operation_getmessage()
test = base64.b64decode(test) # new line here
print("Value of test: ", test)
decryptor = cipher.decryptor()
message_decrypt = decryptor.update(test) + decryptor.finalize()
print("Value of message_decrypted is: ", message_decrypt)
unpadder = padding.PKCS7(128).unpadder()
data = unpadder.update(message_decrypt)
print("Value of message is: ", data + unpadder.finalize())
添加回答
举报