aboutsummaryrefslogtreecommitdiffstats
path: root/lang/python/examples/howto/encrypt-file.py
diff options
context:
space:
mode:
authorBen McGinnes <[email protected]>2018-03-19 22:53:27 +0000
committerBen McGinnes <[email protected]>2018-03-19 22:53:27 +0000
commit7221bb67642eb01a07957d66d0cbcd4ef8aadbf8 (patch)
tree8e7364b5f5c913a06e2b53f799905e409f3b0f48 /lang/python/examples/howto/encrypt-file.py
parentexample: sign and encrypt file (diff)
downloadgpgme-7221bb67642eb01a07957d66d0cbcd4ef8aadbf8.tar.gz
gpgme-7221bb67642eb01a07957d66d0cbcd4ef8aadbf8.zip
example: encrypt file
* Nested encryption in try/except statement in case recipient key is untrusted or invalid.
Diffstat (limited to 'lang/python/examples/howto/encrypt-file.py')
-rwxr-xr-xlang/python/examples/howto/encrypt-file.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/lang/python/examples/howto/encrypt-file.py b/lang/python/examples/howto/encrypt-file.py
index 8aee52ad..017a3421 100755
--- a/lang/python/examples/howto/encrypt-file.py
+++ b/lang/python/examples/howto/encrypt-file.py
@@ -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)