pgpy key.decrypt 没有返回解密的文本
pgpy key.decrypt is not returning decrypted text
我有一段代码应该打开一个 pgp 加密文件,使用私钥对其进行解密,然后将解密后的消息输出到一个文本文件。我已经使用 GnuPG 确认私钥能够解密原始消息和输出消息,所以我相信消息和密钥正在正确加载并且消息正在被解密,但由于某种原因,输出正在重新加密。我是 python 的新手,所以我可能忽略了一些简单的事情。
key, _ = pgpy.PGPKey.from_file(key_file.name)
encrypted_message = pgpy.PGPMessage.from_file(input_file.name)
decrypted_message = key.decrypt(encrypted_message)
str_decrypted_message = str(decrypted_message)
output_file.write(str_decrypted_message)
编辑
这是整个代码,包括生成密钥的函数,以防解密函数之外出现问题。
def pgp_gen(userid, pgpcomment, mail, priv_key_file='priv_key_file.acs', pub_key_file='pub_key_file.gpg'):
key = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
uid = pgpy.PGPUID.new(userid, comment=pgpcomment, email=mail)
key.add_uid(uid, usage={KeyFlags.Sign, KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage},
hashes=[HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512, HashAlgorithm.SHA224],
ciphers=[SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.AES192, SymmetricKeyAlgorithm.AES128],
compression=[CompressionAlgorithm.ZLIB, CompressionAlgorithm.BZ2, CompressionAlgorithm.ZIP, CompressionAlgorithm.Uncompressed])
subkey = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
key.add_subkey(subkey, usage={KeyFlags.Authentication})
keystr = str(key)
pubkeystr = str(key.pubkey)
tmp_file = open(pub_key_file, 'w')
tmp_file.write(pubkeystr)
tmp_file.close()
tmp_file = open(priv_key_file, 'w')
tmp_file.write(keystr)
tmp_file.close()
sys.exit()
def pgp_en(encrypt, input_file, output_file, key_file):
if(encrypt):
key, _ = pgpy.PGPKey.from_file(key_file.name)
file_message = pgpy.PGPMessage.new(input_file.name, file=True)
encrypted_file_message = key.encrypt(file_message)
output_file.write(str(encrypted_file_message))
sys.exit()
else:
key, _ = pgpy.PGPKey.from_file(key_file.name)
encrypted_message = pgpy.PGPMessage.from_file(input_file.name)
decrypted_message = key.decrypt(encrypted_message)
str_decrypted_message = str(decrypted_message)
output_file.write(str_decrypted_message)
sys.exit()
对于偶然发现此问题的任何人,我发现我做错了什么。 This 答案对我有用。好像切换
str_decrypted_message = str(decrypted_message)
至
str_decrypted_message = decrypted_message.message
解决了我的问题。
我有一段代码应该打开一个 pgp 加密文件,使用私钥对其进行解密,然后将解密后的消息输出到一个文本文件。我已经使用 GnuPG 确认私钥能够解密原始消息和输出消息,所以我相信消息和密钥正在正确加载并且消息正在被解密,但由于某种原因,输出正在重新加密。我是 python 的新手,所以我可能忽略了一些简单的事情。
key, _ = pgpy.PGPKey.from_file(key_file.name)
encrypted_message = pgpy.PGPMessage.from_file(input_file.name)
decrypted_message = key.decrypt(encrypted_message)
str_decrypted_message = str(decrypted_message)
output_file.write(str_decrypted_message)
编辑 这是整个代码,包括生成密钥的函数,以防解密函数之外出现问题。
def pgp_gen(userid, pgpcomment, mail, priv_key_file='priv_key_file.acs', pub_key_file='pub_key_file.gpg'):
key = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
uid = pgpy.PGPUID.new(userid, comment=pgpcomment, email=mail)
key.add_uid(uid, usage={KeyFlags.Sign, KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage},
hashes=[HashAlgorithm.SHA256, HashAlgorithm.SHA384, HashAlgorithm.SHA512, HashAlgorithm.SHA224],
ciphers=[SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.AES192, SymmetricKeyAlgorithm.AES128],
compression=[CompressionAlgorithm.ZLIB, CompressionAlgorithm.BZ2, CompressionAlgorithm.ZIP, CompressionAlgorithm.Uncompressed])
subkey = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
key.add_subkey(subkey, usage={KeyFlags.Authentication})
keystr = str(key)
pubkeystr = str(key.pubkey)
tmp_file = open(pub_key_file, 'w')
tmp_file.write(pubkeystr)
tmp_file.close()
tmp_file = open(priv_key_file, 'w')
tmp_file.write(keystr)
tmp_file.close()
sys.exit()
def pgp_en(encrypt, input_file, output_file, key_file):
if(encrypt):
key, _ = pgpy.PGPKey.from_file(key_file.name)
file_message = pgpy.PGPMessage.new(input_file.name, file=True)
encrypted_file_message = key.encrypt(file_message)
output_file.write(str(encrypted_file_message))
sys.exit()
else:
key, _ = pgpy.PGPKey.from_file(key_file.name)
encrypted_message = pgpy.PGPMessage.from_file(input_file.name)
decrypted_message = key.decrypt(encrypted_message)
str_decrypted_message = str(decrypted_message)
output_file.write(str_decrypted_message)
sys.exit()
对于偶然发现此问题的任何人,我发现我做错了什么。 This 答案对我有用。好像切换
str_decrypted_message = str(decrypted_message)
至
str_decrypted_message = decrypted_message.message
解决了我的问题。