使用 wxpython GUI 加密文件
Encrypt a file with wxpython GUI
我想在 Python 中使用 cryptography
加密文件。
我正在使用 wx
模块,它是一个 GUI 库,我的应用程序是这样的:如果用户单击 Encrypt a File
按钮,应用程序打开文件资源管理器并且他有能力选择他要加密的文件。当他点击一个文件并打开它时,我的函数会对其进行加密。
但它不起作用,它什么都不做,我没有收到任何错误,它似乎运行良好,但事实并非如此。
有什么建议吗?
import wx
import os
import random
import ctypes
from cryptography.fernet import Fernet
desktop = os.path.expanduser('~/Desktop')
fileKey = str(random.SystemRandom().randint(100, 1000)) + 'key.txt'
class myFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='Hello!', size=(600, 400), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
panel = wx.Panel(self)
self.text_ctrl = wx.TextCtrl(panel, pos=(5, 5)) # Edit box
EncryptButton = wx.Button(panel, label='Encrypt a File', pos=(475, 295), size=(100, 55)).Bind(wx.EVT_BUTTON, self.onOpen) # Encrypt Button
self.Show() # Show Window
def onOpen(self, event):
fileFormat = 'All Files (*.*) | *.*'
dialog = wx.FileDialog(self, 'Choose File', wildcard=fileFormat, style=wx.FD_OPEN ^ wx.FD_FILE_MUST_EXIST)
path = dialog.GetPath()
if dialog.ShowModal() == wx.ID_OK:
try:
key = Fernet.generate_key()
tokenEnc = Fernet(key)
with open(path, 'rb+') as fobj:
plainText = fobj.read()
cipherText = tokenEnc.encrypt(plainText)
fobj.seek(0)
fobj.truncate()
fobj.write(cipherText)
ctypes.windll.user32.MessageBoxW(0, 'We have Encrypted the File, also, We have created a file with the key in your Desktop.', 'Performed Successfully', 1)
with open(os.path.join(desktop, fileKey), 'wb') as keyFile:
keyFile.write(key)
except Exception as e:
return False
if __name__ == '__main__':
app = wx.App()
frame = myFrame()
app.MainLoop()
问题是这段代码:
path = dialog.GetPath()
if dialog.ShowModal() == wx.ID_OK:
当您甚至没有显示对话框时,您正在询问路径。这导致空字符串。
您需要先显示模态对话框,然后在用户验证文件时获取路径:
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
请注意,此构造不是好的做法,它会阻止您调试 任何可能发生的 错误:
except Exception as e:
return False
至少如果发生不好的事情,打印异常(或使用 wx 对话框向用户显示)
except Exception as e:
print("something bad happened {}".format(e))
return False
我想在 Python 中使用 cryptography
加密文件。
我正在使用 wx
模块,它是一个 GUI 库,我的应用程序是这样的:如果用户单击 Encrypt a File
按钮,应用程序打开文件资源管理器并且他有能力选择他要加密的文件。当他点击一个文件并打开它时,我的函数会对其进行加密。
但它不起作用,它什么都不做,我没有收到任何错误,它似乎运行良好,但事实并非如此。
有什么建议吗?
import wx
import os
import random
import ctypes
from cryptography.fernet import Fernet
desktop = os.path.expanduser('~/Desktop')
fileKey = str(random.SystemRandom().randint(100, 1000)) + 'key.txt'
class myFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='Hello!', size=(600, 400), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
panel = wx.Panel(self)
self.text_ctrl = wx.TextCtrl(panel, pos=(5, 5)) # Edit box
EncryptButton = wx.Button(panel, label='Encrypt a File', pos=(475, 295), size=(100, 55)).Bind(wx.EVT_BUTTON, self.onOpen) # Encrypt Button
self.Show() # Show Window
def onOpen(self, event):
fileFormat = 'All Files (*.*) | *.*'
dialog = wx.FileDialog(self, 'Choose File', wildcard=fileFormat, style=wx.FD_OPEN ^ wx.FD_FILE_MUST_EXIST)
path = dialog.GetPath()
if dialog.ShowModal() == wx.ID_OK:
try:
key = Fernet.generate_key()
tokenEnc = Fernet(key)
with open(path, 'rb+') as fobj:
plainText = fobj.read()
cipherText = tokenEnc.encrypt(plainText)
fobj.seek(0)
fobj.truncate()
fobj.write(cipherText)
ctypes.windll.user32.MessageBoxW(0, 'We have Encrypted the File, also, We have created a file with the key in your Desktop.', 'Performed Successfully', 1)
with open(os.path.join(desktop, fileKey), 'wb') as keyFile:
keyFile.write(key)
except Exception as e:
return False
if __name__ == '__main__':
app = wx.App()
frame = myFrame()
app.MainLoop()
问题是这段代码:
path = dialog.GetPath()
if dialog.ShowModal() == wx.ID_OK:
当您甚至没有显示对话框时,您正在询问路径。这导致空字符串。
您需要先显示模态对话框,然后在用户验证文件时获取路径:
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
请注意,此构造不是好的做法,它会阻止您调试 任何可能发生的 错误:
except Exception as e:
return False
至少如果发生不好的事情,打印异常(或使用 wx 对话框向用户显示)
except Exception as e:
print("something bad happened {}".format(e))
return False