当我尝试使用下面的cmd在shell中解密此文件时:
gpg test.zip.asc
在给出传递后,我在同一文件夹中获得解密文件"test.zip"
,但是使用python脚本我无法获得解密文件。没有错误,对我做错了什么想法?
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()
答案 0 :(得分:1)
请参阅docs for gnupg:
要解密消息,请使用以下方法:
>>> decrypted_data = gpg.decrypt(data)
模块不解密文件并将其保存在与原始文件相同的目录中,如gpg二进制文件。相反,它返回一串解密数据。
但是,您还有另一个问题。 gpg.decrypt()
正在尝试解密encrpyted_file
中存储的值,当然这只是字符串"test.zip.asc"
。相反,您希望使用该名称解密文件的内容。为此,您需要使用gpg.decrypt_file()
,如下所示:
# 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)
答案 1 :(得分:0)
decrypt_file方法允许提供到输出文件的路径,如下所示:
from tkinter import *
x = 1
y = 1
z = 0
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
textVar = StringVar()
self.button = Button(
frame, textvariable=textVar, command=textVar.set(str(fibonacci()))
)
self.button.pack()
def fibonacci():
global x, y, z
z = x
x = x + y
y = z
return x
root = Tk()
app = App(root)
root.mainloop()