Skip to content

Instantly share code, notes, and snippets.

@marteinn
Last active February 7, 2025 07:56
Show Gist options
  • Save marteinn/0739d62b8a1568e18867d68b609c44a6 to your computer and use it in GitHub Desktop.
Save marteinn/0739d62b8a1568e18867d68b609c44a6 to your computer and use it in GitHub Desktop.
Example on how to sign and encrypt messages in PGPY
import pgpy
# Keys
encryption_pubkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-encryption-public-key.asc')
encryption_privkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-encryption-secret-key.pgp')
signing_pubkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-signing-public-key.asc')
signing_privkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-signing-secret-key.pgp')
message = pgpy.PGPMessage.new("My test message")
# Sign and encrypt
with signing_privkey.unlock("signkey"):
message |= signing_privkey.sign(message)
encrypted_message = encryption_pubkey.encrypt(message)
# Decrypt
with encryption_privkey.unlock("testkey"):
decrypted_message = encryption_privkey.decrypt(encrypted_message)
print(decrypted_message.message)
# Verify
assert bool(signing_pubkey.verify(decrypted_message)) == True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment