【发布时间】:2021-01-22 06:08:09
【问题描述】:
我正在开发 python GUI,它可以加密用户的输入并将加密的消息存储在一个新文件中。我在尝试将 python 脚本中的 input() 实现到 wxpython GUI 时遇到问题。将不胜感激。
下面是我的python加密脚本(公钥和私钥是从另一个.py文件生成的):
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()
这是我目前在 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()
【问题讨论】:
-
所以您希望用户通过 wx.TextCtrl() 发送一些输入?
-
是的,可以用简单的 wxpython 代码吗?
-
您可能还希望从
Generate.py的命令行中接受密码。对于import sys并使用myargs = sys.argv[1:]之类的东西,它将给出您传递给Generate.py的参数的list。安全说明:参数在clear中传递。