简体   繁体   中英

Encrypt a file with wxpython GUI

I want to Encrypt a file using cryptography in Python.

I'm using wx module, which is a GUI library, my application is like this: if the user clicks on Encrypt a File button, the application open file explorer and he have the ability to choose which file he want to encrypt. When he click on a file and open it, my function will encrypt it.

But it does not work, it just doing nothing, I'm not getting any errors and it seems to work well, but it is not.

Any Suggestions ?

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()

problem is this code:

    path = dialog.GetPath()
    if dialog.ShowModal() == wx.ID_OK:

you're asking for the path when you didn't even show the dialog. This results in an empty string.

You need to first show modal dialog, then get the path if user validated the file:

    if dialog.ShowModal() == wx.ID_OK:
        path = dialog.GetPath()

note that this construct isn't good practice and prevents you to debug any error that can happen:

   except Exception as e:
        return False

at least if something bad happens, print the exception (or use a wx dialog to show it to the user)

   except Exception as e:
        print("something bad happened {}".format(e))
        return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM