为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用密码术从用户输入的 python 中解密消息?

如何使用密码术从用户输入的 python 中解密消息?

炎炎设计 2021-07-21 17:31:31
我能够解密运行脚本的消息,但当我使用用户输入法获取加密消息时无法解密。我有这个简单的脚本:from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modesfrom cryptography.hazmat.backends import default_backendfrom cryptography.hazmat.primitives import paddingpadder = padding.PKCS7(128).padder()backend = default_backend()def operation_getmessage():    messagesinput = input("Please, insert messages to decrypt: ")    messagesinput = bytes(messagesinput, "utf-8")    message = padder.update(messagesinput)    message += padder.finalize()    return messagekeyinput = 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:", message_encrypt)test = operation_getmessage()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())
查看完整描述

2 回答

?
Helenr

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())



查看完整回答
反对 回复 2021-07-28
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

查看文档

之后finalize()被称为该对象可以不再被使用; update()并且finalize()将引发一个AlreadyFinalized例外。

尝试初始化padder函数内部operation_getmessage(),以便每次调用函数时它都是一个新的填充器。

def operation_getmessage():
    padder = padding.PKCS7(128).padder()
    ...
    return message


查看完整回答
反对 回复 2021-07-28
  • 2 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信