Blame lang/python/examples/howto/decrypt-file.py

Packit Service 30b792
#!/usr/bin/env python3
Packit Service 30b792
# -*- coding: utf-8 -*-
Packit Service 30b792
Packit Service 30b792
from __future__ import absolute_import, division, unicode_literals
Packit Service 30b792
Packit Service 30b792
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify it under
Packit Service 30b792
# the terms of the GNU General Public License as published by the Free Software
Packit Service 30b792
# Foundation; either version 2 of the License, or (at your option) any later
Packit Service 30b792
# version.
Packit Service 30b792
#
Packit Service 30b792
# This program is free software; you can redistribute it and/or modify it under
Packit Service 30b792
# the terms of the GNU Lesser General Public License as published by the Free
Packit Service 30b792
# Software Foundation; either version 2.1 of the License, or (at your option)
Packit Service 30b792
# any later version.
Packit Service 30b792
#
Packit Service 30b792
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit Service 30b792
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Packit Service 30b792
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
Packit Service 30b792
# Lesser General Public License for more details.
Packit Service 30b792
#
Packit Service 30b792
# You should have received a copy of the GNU General Public License and the GNU
Packit Service 30b792
# Lesser General Public along with this program; if not, see
Packit Service 30b792
# <https://www.gnu.org/licenses/>.
Packit Service 30b792
Packit Service 30b792
import gpg
Packit Service 30b792
import sys
Packit Service 30b792
Packit Service 30b792
if len(sys.argv) == 3:
Packit Service 30b792
    ciphertext = sys.argv[1]
Packit Service 30b792
    newfile = sys.argv[2]
Packit Service 30b792
elif len(sys.argv) == 2:
Packit Service 30b792
    ciphertext = sys.argv[1]
Packit Service 30b792
    newfile = input("Enter path and filename to save decrypted data to: ")
Packit Service 30b792
else:
Packit Service 30b792
    ciphertext = input("Enter path and filename of encrypted file: ")
Packit Service 30b792
    newfile = input("Enter path and filename to save decrypted data to: ")
Packit Service 30b792
Packit Service 30b792
with open(ciphertext, "rb") as cfile:
Packit Service 30b792
    try:
Packit Service 30b792
        plaintext, result, verify_result = gpg.Context().decrypt(cfile)
Packit Service 30b792
    except gpg.errors.GPGMEError as e:
Packit Service 30b792
        plaintext = None
Packit Service 30b792
        print(e)
Packit Service 30b792
Packit Service 30b792
if plaintext is not None:
Packit Service 30b792
    with open(newfile, "wb") as nfile:
Packit Service 30b792
        nfile.write(plaintext)
Packit Service 30b792
else:
Packit Service 30b792
    pass