Search code examples
pythonpython-2.7gnupgpgp

Get .txt file from the available .pgp file(Data) and .asc file(Key)


Using Python 2.17.12, Pycharm and Linux Ubuntu

Want to know how can I decrypt .pgp file to .txt file, using a key(.asc file) in Python Script.

Able to do it in python command line, but want to write a script for it.


Solution

  • import gnupg
    
    gpg = gnupg.GPG(gnupghome='/path/to/directory')
    

    imported key

    key_to_import = '.asc key file'
    key_data = open(key_to_import).read()
    import_result = gpg.import_keys(key_data)
    

    decrypt File

    with open('.pgp file name', 'rb') as f:
        status = gpg.decrypt_file(f,passphrase='**appropriate_one**', output='.txt file name')
    

    check status

    print 'ok: ', status.ok
    print 'status: ', status.status
    print 'stderr: ', status.stderr
    

    Worked for me, might help someone else.