Reputation: 15
When i am trying to decrypt this file in shell using cmd below:
gpg test.zip.asc
After giving the pass I am getting decrypted file "test.zip"
in same folder but using python script I am not able to get the decrypted file. No error occurs, any idea on what I'm doing wrong ?
import gnupg
def testgpg():
print "testgpg function started"
encrypted_file = "test.zip.asc"
passwd = "passwd"
gpg = gnupg.GPG(gnupghome='/home/centos/.gnupg')
try:
print "trying"
gpg.decrypt(encrypted_file, passphrase=passwd)
print "decrypted"
except Exception as e:
print "not decrypted: -->", str(e)
testgpg()
Upvotes: 1
Views: 4529
Reputation: 1578
The decrypt_file method allows to provide a path to a output file like so:
gpg = gnupg.GPG(gnupghome=config.gnupgHome)
with open(encryptedZippedApp, 'rb') as src:
status = gpg.decrypt_file(src, output='/tmp/myout.zip')
print('ok: ', status.ok)
Upvotes: 0
Reputation: 15903
See the docs for gnupg:
To decrypt a message, use the following approach:
>>> decrypted_data = gpg.decrypt(data)
The module doesn't decrypt the file and save it in the same directory as the original file like the gpg binary. Instead it returns a string of the decrypted data.
However, you also have another problem. gpg.decrypt()
is trying to decrypt the value stored in encrpyted_file
, which of course is just the string "test.zip.asc"
. Instead, you want to decrypt the contents of the file with that name. For that, you need to use gpg.decrypt_file()
like so:
# Open the encrypted file (gpg will handle reading it)
with open(encrypted_file, 'rb') as src:
# decrypt the file and store its decrypted contents
decrypted_contents = gpg.decrypt_file(src, passphrase=passwd)
# write the decrypted contents to a new file
with open('destination_file.zip', 'wb') as dst:
dst.write(decrypted_contents)
Upvotes: 1