Blame lang/python/examples/howto/symcrypt-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
import gpg
Packit Service 30b792
import sys
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
"""
Packit Service 30b792
Symmetrically encrypts a file.  Passphrase will be prompted for via Pinentry.
Packit Service 30b792
Packit Service 30b792
Will produce both an ASCII armoured and GPG binary format copy of the encrypted
Packit Service 30b792
file.
Packit Service 30b792
"""
Packit Service 30b792
Packit Service 30b792
if len(sys.argv) > 2:
Packit Service 30b792
    filename = " ".join(sys.argv[1:])
Packit Service 30b792
elif len(sys.argv) == 2:
Packit Service 30b792
    filename = sys.argv[1]
Packit Service 30b792
else:
Packit Service 30b792
    filename = input("Enter the path and filename to encrypt: ")
Packit Service 30b792
Packit Service 30b792
with open(filename, "rb") as f:
Packit Service 30b792
    text = f.read()
Packit Service 30b792
Packit Service 30b792
with gpg.Context(armor=True) as ca:
Packit Service 30b792
    try:
Packit Service 30b792
        ciphertext, result, sign_result = ca.encrypt(text, passphrase=None,
Packit Service 30b792
                                                     sign=False)
Packit Service 30b792
        with open("{0}.asc".format(filename), "wb") as fa:
Packit Service 30b792
            fa.write(ciphertext)
Packit Service 30b792
    except gpg.errors.GPGMEError as e:
Packit Service 30b792
        print(e)
Packit Service 30b792
Packit Service 30b792
with gpg.Context() as cg:
Packit Service 30b792
    try:
Packit Service 30b792
        ciphertext, result, sign_result = cg.encrypt(text, passphrase=None,
Packit Service 30b792
                                                     sign=False)
Packit Service 30b792
        with open("{0}.gpg".format(filename), "wb") as fg:
Packit Service 30b792
            fg.write(ciphertext)
Packit Service 30b792
    except gpg.errors.GPGMEError as e:
Packit Service 30b792
        print(e)