pobcrypt/pobcrypt-gui/pobcrypt-gui.py


Home Back

#!/usr/bin/env python3


from cryptography.fernet import Fernet
import sys,os,wx
from wx import xrc, MessageDialog, FileDialog
import threading

def generateKey(f):
    key = Fernet.generate_key()
 
    with open(f, 'wb') as filekey:
        filekey.write(key)

def enc(mkey,mfile):
    with open(mkey, 'rb') as filekey:
        key = filekey.read()
     
    fernet = Fernet(key)
    with open(mfile, 'rb') as file:
        original = file.read()
         
    encrypted = fernet.encrypt(original)
     
    with open(mfile+'.enc', 'wb') as encrypted_file:
        encrypted_file.write(encrypted)

def dec(mkey,mfile):
    with open(mkey, 'rb') as filekey:
        key = filekey.read()

    fernet = Fernet(key)
 
    with open(mfile, 'rb') as enc_file:
        encrypted = enc_file.read()
 
    decrypted = fernet.decrypt(encrypted)
    name=os.path.splitext(mfile)[0] 
    with open(name, 'wb') as dec_file:
        dec_file.write(decrypted)



class MainApp(wx.App):
    
    def OnInit(self):
        if getattr(sys, 'frozen', False):
            self.res = xrc.XmlResource(os.path.join(sys._MEIPASS, "files/tgui.xrc"))
        else:
            self.res = xrc.XmlResource('tgui.xrc')
        
        
        self.init_frame()
        return True
        
    def init_frame(self):
        self.frmMain = self.res.LoadFrame(None, 'TMainFrame')
        
        
        if getattr(sys, 'frozen', False):
            self.frmMain.SetIcon(wx.Icon(os.path.join(sys._MEIPASS, "files/lock.ico")))
        else:
            self.frmMain.SetIcon(wx.Icon("lock.ico"))
        
        
        
        
        self.btStart = xrc.XRCCTRL(self.frmMain, 'btStart')
        self.frmMain.Bind(wx.EVT_BUTTON, self.onClickStart, id=xrc.XRCID('btStart'))
        
        self.btGenKey = xrc.XRCCTRL(self.frmMain, 'btGenKey')
        self.frmMain.Bind(wx.EVT_BUTTON, self.onClickGenKey, id=xrc.XRCID('btGenKey'))
        
        self.btAbout = xrc.XRCCTRL(self.frmMain, 'btAbout')
        self.frmMain.Bind(wx.EVT_BUTTON, self.onClickAbout, id=xrc.XRCID('btAbout'))
        
        self.gauge = xrc.XRCCTRL(self.frmMain, 'gauge')
        self.gauge.Show(False)
        
        self.cmbMode=xrc.XRCCTRL(self.frmMain, 'cmbMode')
        self.lblStatus=xrc.XRCCTRL(self.frmMain, 'lblStatus')
        
        self.filePicker=xrc.XRCCTRL(self.frmMain, 'filePicker')
        self.filePickerKey=xrc.XRCCTRL(self.frmMain, 'filePickerKey')
        
        
        self.frmMain.Show()
    
    def onClickAbout(self,evt):
        msg="Made with Love by Fabio Di Matteo - pobfdm@gmail.com"
        p=MessageDialog(self.frmMain, msg, caption="About pobcrypt")
        p.ShowModal()
            
    def onClickGenKey(self,evt):
        
        fd=FileDialog(self.frmMain, message="Enter a file where save the key",
            defaultDir="", defaultFile="" )
        fd.ShowModal()
        path=fd.GetPath()
        generateKey(path)
        
    def onClickStart(self,evt):
        
        mainThd = threading.Thread(target=self.startTask)
        mainThd.daemon = True
        mainThd.start()
    
    
    
        
    def startTask(self):
        wx.CallAfter(self.gauge.Show, True)
        wx.CallAfter(self.gauge.Pulse)
        wx.CallAfter(self.btStart.Disable)
        
        if (self.cmbMode.GetSelection()==0 ): mode="enc";
        if (self.cmbMode.GetSelection()==1 ): mode="dec";
        
        file=self.filePicker.GetPath()
        key=self.filePickerKey.GetPath()
        
        if (mode=="enc"):
            try:
                enc(key,file)
            except :
                msg="Encrypt error..."
                p=MessageDialog(self.frmMain, msg, caption="Warning")
                p.ShowModal()
                    
        elif (mode=="dec"):
            try:
                dec(key,file)    
            except:
                msg="Decrypt error..."
                p=MessageDialog(self.frmMain, msg, caption="Warning")
                p.ShowModal()
        
        wx.CallAfter(self.gauge.Show, False)
        wx.CallAfter(self.gauge.SetValue,0)
        status="End task: %s" % mode
        self.lblStatus.SetLabel(status)
        wx.CallAfter(self.btStart.Enable)
        print(status)
        

if __name__ == '__main__':
    app = MainApp(False)
    app.MainLoop()

Powered by Code, a simple repository browser by Fabio Di Matteo