1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4from __future__ import absolute_import, division, unicode_literals
5
6# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
7#
8# This program is free software; you can redistribute it and/or modify it under
9# the terms of the GNU General Public License as published by the Free Software
10# Foundation; either version 2 of the License, or (at your option) any later
11# version.
12#
13# This program is free software; you can redistribute it and/or modify it under
14# the terms of the GNU Lesser General Public License as published by the Free
15# Software Foundation; either version 2.1 of the License, or (at your option)
16# any later version.
17#
18# This program is distributed in the hope that it will be useful, but WITHOUT
19# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
21# Lesser General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License and the GNU
24# Lesser General Public along with this program; if not, see
25# <https://www.gnu.org/licenses/>.
26
27import gpg
28import sys
29
30if len(sys.argv) == 3:
31    ciphertext = sys.argv[1]
32    newfile = sys.argv[2]
33elif len(sys.argv) == 2:
34    ciphertext = sys.argv[1]
35    newfile = input("Enter path and filename to save decrypted data to: ")
36else:
37    ciphertext = input("Enter path and filename of encrypted file: ")
38    newfile = input("Enter path and filename to save decrypted data to: ")
39
40with open(ciphertext, "rb") as cfile:
41    try:
42        plaintext, result, verify_result = gpg.Context().decrypt(cfile)
43    except gpg.errors.GPGMEError as e:
44        plaintext = None
45        print(e)
46
47if plaintext is not None:
48    with open(newfile, "wb") as nfile:
49        nfile.write(plaintext)
50else:
51    pass
52