1
votes

I'm working on python GUI that can encrypt user's input and storing the encrypted message in a new file. I have problem trying to implement input() from python script into wxpython GUI. Would appreciate any help.

Below is my python script for encryption (public and private key is generated from another .py file):

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP

data = input("Enter message: ").encode("utf-8")
file_out = open("encrypted_data.bin", "wb")

recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = get_random_bytes(16)

# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
enc_session_key = cipher_rsa.encrypt(session_key)

# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
file_out.close()

And this is my current working on wxpython GUI:

import wx
import subprocess
import os

class MyFrame(wx.Frame):    
    def __init__(self, parent, title):
        super().__init__(parent, title=title)
        self.panel = MyPanel(self)

class MyPanel(wx.Panel):
    def __init__(self, parent): 
        super(MyPanel,self).__init__(parent)
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)

        self.genBtn = wx.Button(self, label = "Generate Keys")
        hbox.Add(self.genBtn, wx.EXPAND)
        self.genBtn.Bind(wx.EVT_BUTTON, self.genProc)
        self.SetSizer(hbox)

        self.sendBtn = wx.Button(self, label="Send Message")
        hbox.Add(self.sendBtn, wx.BOTTOM)
        self.genBtn.Bind(wx.EVT_BUTTON, self.genProc)
        self.SetSizer(hbox)


    def genProc(self, event):
        p = subprocess.Popen(["python", "-u", "Generate.py"], stdout=subprocess.PIPE, bufsize=-1)
        self.pid = p.pid
        wx.MessageBox("Public and Private Keys Generated", 'Dialog', wx.OK | wx.ICON_INFORMATION)
        #wx.MessageBox("Message Box Icon Warning", 'Dialog', wx.OK | wx.ICON_WARNING)
        #wx.MessageBox("Message Box Dialog Error", 'Dialog', wx.OK | wx.ICON_ERROR)
        
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(parent=None, title="Sender")
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True
        
def main():
    app = MyApp()
    app.MainLoop()

if __name__ == '__main__':
    main()
1
so you want the user to send some input via wx.TextCtrl()? - Leonardo Scotti
Yes is it possible with simple wxpython code? - Ruch
You may also be looking to accept the password from the command line in Generate.py. For that import sys and use something like myargs = sys.argv[1:] which will give a list of the arguments you passed to Generate.py. Security note: the arguments are being passed in the clear. - Rolf of Saxony

1 Answers

0
votes

Weell, what you need is a semple binding:

self.yourtestctrl.Bind(wx.EVT_TEXT_ENTER, self.OnTextEnter)

Note:

to do this you need to add wx.TE_PROCESS_ENTER to yourtextcontrol's style

done this you have to create your function:

def OnTextEnter(self, event):
    
    text = [self.yourtextevetn.GetLineText(line)for line in self.yourtextctrl.GetNumberOfLines()]

    text = "\n".join("text")

    #here you have your input and you can store it o call a function with it

Note:

if you want the user to send the input when he wants you can create a wx.Button and then bind it to the above function