example: encrypt file

* Nested encryption in try/except statement in case recipient key is
  untrusted or invalid.
This commit is contained in:
Ben McGinnes 2018-03-20 09:53:27 +11:00
parent f3fe47e8fd
commit 7221bb6764

View File

@ -52,13 +52,19 @@ with open(filename, "rb") as f:
text = f.read()
with gpg.Context(armor=True) as ca:
ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey,
sign=False)
with open("{0}.asc".format(filename), "wb") as fa:
fa.write(ciphertext)
try:
ciphertext, result, sign_result = ca.encrypt(text, recipients=rkey,
sign=False)
with open("{0}.asc".format(filename), "wb") as fa:
fa.write(ciphertext)
except gpg.errors.InvalidRecipients as e:
print(e)
with gpg.Context() as cg:
ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey,
sign=False)
with open("{0}.gpg".format(filename), "wb") as fg:
fg.write(ciphertext)
try:
ciphertext, result, sign_result = cg.encrypt(text, recipients=rkey,
sign=False)
with open("{0}.gpg".format(filename), "wb") as fg:
fg.write(ciphertext)
except gpg.errors.InvalidRecipients as e:
print(e)