Monday 8 September 2014

Python Gnupg Decrypt Files


GnuPG needs to import private key and passphrase to decrypt files.
GnuPG works on the basis of a “home directory” which is used to store public and private keyring files as well as a trust database.


# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import time
import zipimport
importer = zipimport.zipimporter('gnupg.mod')
gnupg = importer.load_module('gnupg')

gpg = gnupg.GPG(gnupghome=os.getcwd())

key_data = open('private.key').read()
import_result = gpg.import_keys(key_data)

with open('data.gpg', 'rb') as f:
    status = gpg.decrypt_file(f, passphrase='my passphrase', always_trust=True, output='data')

print(status.ok) 
print(status.status) 
print(status.stderr)

Reference:
https://pythonhosted.org/python-gnupg/
http://www.saltycrane.com/blog/2011/10/python-gnupg-gpg-example/

No comments:

Post a Comment