Reputation: 21
I am trying out PGPy and I get an error whenever I encrypt or decrypt a message.
These are the codes that I use. Documentation and examples can be found here
Code to encrypt a message using the public key:
import pgpy
key_pub = '''BEGIN PUBLIC KEY BLOCK...END PUBLIC KEY BLOCK'''.lstrip()
message = "It worked!"
# import ASCII formatted public key
pub_key = pgpy.PGPKey()
pub_key.parse(key_pub)
# create new message
text_message = pgpy.PGPMessage.new(message)
# encrypt a message using pub key
encrypted_message = pub_key.encrypt(text_message)
print(encrypted_message)
This one gave the output but with a message
UserWarning: Selected compression algorithm not in key preferences
encrypted_message = pub_key.encrypt(text_message)
which I don't get why.
Code to decrypt the message using the priv key:
import pgpy
key_priv ='''*BEGIN PRIV KEY BLOCK...END PRIV KEY BLOCK'''.lstrip()
cipher_text = '''BEGIN PGP MESSAGE...END PGP MESSAGE'''.lstrip()
# import ASCII formatted private key
priv_key = pgpy.PGPKey()
priv_key.parse(key_priv)
message_from_blob = pgpy.PGPMessage.from_blob(cipher_text)
# decrypts a message using priv key
decrypted_message = priv_key.decrypt(cipher_text)
print(decrypted_message)
And this one does not work at all.This is the error I get.
Traceback (most recent call last):
File "C:/Users..practice.py", line 13, in <module>
decrypted_message = priv_key.decrypt(cipher_text)
File "C:\Users...venv\lib\site-packages\pgpy\decorators.py", line 126, in _action
self.check_attributes(key)
File "C:\Users...venv\lib\site-packages\pgpy\decorators.py", line 111, in check_attributes
raise PGPError("Expected: {attr:s} == {eval:s}. Got: {got:s}"
pgpy.errors.PGPError: Expected: is_unlocked == True. Got: False
Upvotes: 1
Views: 11013
Reputation: 484
PUBLIC_KEY_FILE = 'path/to/keyfile/my_pub_key.asc'
pub_key, _ = pgpy.PGPKey.from_file(str(PUBLIC_KEY_FILE))
# Encrypt string
txt_msg = pgpy.PGPMessage.new("Hello PGPy World")
print('txt_msg.is_encrypted')
print(txt_msg.is_encrypted)
print('txt_msg.message')
print(txt_msg.message)
encrypted_txt_msg = pub_key.encrypt(txt_msg)
print('encrypted_txt_msg.is_encrypted')
print(encrypted_txt_msg.is_encrypted)
print('encrypted_txt_msg.message')
print(encrypted_txt_msg.message)
# Encrypt file
f_t_e = pgpy.PGPMessage.new(str(FILE_TO_ENCRYPT),file=True)
print('f_t_e.is_encrypted')
print(f_t_e.is_encrypted)
encrypted_f_t_e = pub_key.encrypt(f_t_e)
print('encrypted_f_t_e.is_encoded')
print(encrypted_f_t_e.is_encrypted)
# Load passphrase from file
with open(PASSPHRASE_FILE,'r') as ppfp:
PASSPHRASE = ppfp.readline().replace('\n','')
print(PASSPHRASE)
# Load private key
PRIVATE_KEY_FILE='path/to/keyfile/my_prv_key.gpg'
prv_key, _ = pgpy.PGPKey.from_file(str(PRIVATE_KEY_FILE))
# Unlock private key
print(prv_key.is_protected) # Should be True
with prv_key.unlock(PASSPHRASE):
print(prv_key.is_unlocked) #Should be True
# Decrypt string
decrypted_txt_msg = prv_key.decrypt(encrypted_txt_msg)
print('decrypted_txt_msg.is_encrypted')
print(decrypted_txt_msg.is_encrypted)
print('decrypted_txt_msg.message')
print(decrypted_txt_msg.message)
# Decrypt file
decrypted_f_t_e = prv_key.decrypt(encrypted_f_t_e)
print('decrypted_f_t_e.is_encrypted')
print(decrypted_f_t_e.is_encrypted)
Upvotes: 4