Skip to content

Instantly share code, notes, and snippets.

@chrisdpa-tvx
Created May 31, 2017 10:57
Show Gist options
  • Save chrisdpa-tvx/c497b3eac2db4ece739bf16af9fe6168 to your computer and use it in GitHub Desktop.
Save chrisdpa-tvx/c497b3eac2db4ece739bf16af9fe6168 to your computer and use it in GitHub Desktop.
Encrypt+Sign then decrypt using gpg
import gnupg
import cStringIO
gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
me = {'name_real': 'alice',
'name_email': 'alice@example.com',
'expire_date': '2024-04-01',
'passphrase': 'alicespassword'}
input_data = gpg.gen_key_input(**me)
key = gpg.gen_key(input_data)
alice_fp = key.fingerprint
with open('alice_private.asc', 'w') as f:
f.write(gpg.export_keys(str(key), True))
bob = {'name_real': 'Bob',
'name_email': 'bob@example.com',
'expire_date': '2024-04-01',
'passphrase': 'bobspassword'}
input_data = gpg.gen_key_input(**bob)
key = gpg.gen_key(input_data)
bob_fp = key.fingerprint
out = cStringIO.StringIO()
out.write('The quick brown fox jumps over the lazy dog')
e = gpg.encrypt(
out.getvalue(),
bob_fp,
symmetric=False,
default_key=alice_fp,
passphrase='alicespassword',
armor=False)
print e.status, e.stderr
d = gpg.decrypt(e.data, passphrase='bobspassword')
print d.status, d.stderr, d.data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment