我想创建一个程序,该程序可以加密和解密消息,还可以创建pgp公钥和私钥。加密过程和创建密钥的过程起作用。但是,解密过程不起作用,因为pgpy出于某种原因无法识别该邮件是加密的。
最小示例:
def decrypt(private_key_file,secret_phrase,emsg):
print("message is encrypted: "+str(emsg.is_encrypted))
private_key,_=pgpy.PGPKey.from_file(private_key_file)
with private_key.unlock(secret_phrase):
return str(private_key.decrypt(emsg))
message_to_encode_or_decode=input("Please enter the message to decode: ")
emsg=pgpy.PGPMessage.new(message_to_encode_or_decode,file=is_from_file)
#In this line, you can see that emsg.is_encrypted returns False and I can't figure out why
print("Message: \n"+str(emsg.is_encrypted)+"\n"+message_to_encode_or_decode)
private_key_file=input("Please enter the path to your private key file: ")
password=getpass.getpass("please enter the key's pass phrase: ")
decrypted=decrypt(private_key_file,password,emsg)
print(decrypted)
我希望收到我加密的消息,但是我收到了原始消息。我检查了pgpy的源代码,问题似乎是该邮件未被识别为已加密。
答案 0 :(得分:1)
我知道您发布这个问题已经有一段时间了,但它帮助我解决了我自己的一些 PGPy 问题,我认为如果我发布可能的答案,它可能对下一个找到它的人有用。
>import pgpy
import getpass
def decrypt(private_key_file,secret_phrase,emsg):
print("message is encrypted: "+str(emsg.is_encrypted))
private_key,_=pgpy.PGPKey.from_file(private_key_file)
with private_key.unlock(secret_phrase):
return ((private_key.decrypt(emsg)).message).decode('utf-8')
message_to_encode_or_decode=input("Please enter the message to decode: ")
emsg=pgpy.PGPMessage.from_file(message_to_encode_or_decode)
#In this line, you can see that emsg.is_encrypted returns False and I can't figure out why
print("Message: \nEncrypted (True/False): "+str(emsg.is_encrypted)+"\nFilename: "+message_to_encode_or_decode)
private_key_file=input("Please enter the path to your private key file: ")
password=getpass.getpass("please enter the key's pass phrase: ")
decrypted=decrypt(private_key_file,password,emsg)
print(decrypted)
没有 `decode('utf-8') 我的样本返回的是字节数组而不是字符串。