diff options
author | Ben McGinnes <[email protected]> | 2018-03-19 22:53:27 +0000 |
---|---|---|
committer | Ben McGinnes <[email protected]> | 2018-03-19 22:53:27 +0000 |
commit | 7221bb67642eb01a07957d66d0cbcd4ef8aadbf8 (patch) | |
tree | 8e7364b5f5c913a06e2b53f799905e409f3b0f48 /lang/python/examples/howto | |
parent | example: sign and encrypt file (diff) | |
download | gpgme-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')
-rwxr-xr-x | lang/python/examples/howto/encrypt-file.py | 22 |
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) |