2019-06-11

import wx
from rc4 import decrypt, encrypt
import rsa
import base64
app = wx.App()
window = wx.Frame(None, title="RC4和RSA加密实验    2016082311叶永兴", size=(960, 700))
panel = wx.Panel(window)

#####################################   RC4   #################################################################3
titleMinwin = wx.StaticText(panel, pos=(100, 10), label="待加密")
textMinwen = wx.TextCtrl(panel,  pos=(20, 30), size=(200, 100), style=wx.TE_MULTILINE)

titleMiyao = wx.StaticText(panel, pos=(100, 180), label="秘钥")
textMiyao = wx.TextCtrl(panel,  pos=(20, 200), size=(200, 100), style=wx.TE_MULTILINE)

btnJiami = wx.Button(panel, -1, "加密", pos=(70, 320))
titleMiwein = wx.StaticText(panel, pos=(100, 350), label="密文")
textMiwen = wx.TextCtrl(panel,  pos=(20, 370), size=(200, 100), style=wx.TE_MULTILINE)

btnJiemi = wx.Button(panel, -1, "解密", pos=(70, 490))
titleJmMingwen = wx.StaticText(panel, pos=(100, 520), label="明文")
textJmMingwen = wx.TextCtrl(panel,  pos=(20, 540), size=(200, 100), style=wx.TE_MULTILINE)

def rc4Jiami(event):
    textMiwen.SetValue(value='')
    data = textMinwen.GetValue()
    key  = textMiyao.GetValue()
    miwen = encrypt(data, key)
    textMiwen.SetValue(value=miwen)

def rc4Jiemi(event):
    textJmMingwen.SetValue(value='')
    key  = textMiyao.GetValue()
    miwen = textMiwen.GetValue()
    mingwen = decrypt(miwen, key)
    textJmMingwen.SetValue(value=mingwen)

btnJiami.Bind(wx.EVT_BUTTON, rc4Jiami)
btnJiemi.Bind(wx.EVT_BUTTON, rc4Jiemi)
#############################################################DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD#####33
RSAscreenX=350

btnShengcheng = wx.Button(panel, -1, "生成公私秘钥", pos=(RSAscreenX, 60))
titleGongyao = wx.StaticText(panel, pos=(RSAscreenX+150, 20), label="公钥")
textGongyao = wx.TextCtrl(panel,  pos=(RSAscreenX+130, 40), size=(200, 100), style=wx.TE_MULTILINE)

titleSyao = wx.StaticText(panel, pos=(RSAscreenX+450, 20), label="私钥")
textSiyao = wx.TextCtrl(panel,  pos=(RSAscreenX+350, 40), size=(200, 100), style=wx.TE_MULTILINE)

btnRsaJiami = wx.Button(panel, -1, "加密", pos=(RSAscreenX, 220))
titleRsaMiwen = wx.StaticText(panel, pos=(RSAscreenX+150, 180), label="密文")
textRsaMiwen = wx.TextCtrl(panel,  pos=(RSAscreenX+130, 200), size=(400, 100), style=wx.TE_MULTILINE)

btnRsaJiemi = wx.Button(panel, -1, "解密", pos=(RSAscreenX, 390))
titleRsaMinwen = wx.StaticText(panel, pos=(RSAscreenX+150, 350), label="明文")
textRsaMinwen = wx.TextCtrl(panel,  pos=(RSAscreenX+130, 370), size=(400, 100), style=wx.TE_MULTILINE)


btnRsaQianming = wx.Button(panel, -1, "签名", pos=(RSAscreenX, 550))
btnRsaYanzheng = wx.Button(panel, -1, "验证", pos=(RSAscreenX, 600))
titleQianming = wx.StaticText(panel, pos=(RSAscreenX+150, 520), label="签名信息")
textQianming = wx.TextCtrl(panel,  pos=(RSAscreenX+130, 540), size=(400, 100), style=wx.TE_MULTILINE)
def rsaShengcheng(event):
    (pubkey, privkey) = rsa.newkeys(1024)
    textGongyao.SetValue(value='')
    textSiyao.SetValue(value='')
    textGongyao.SetValue(value=pubkey.save_pkcs1().decode())
    textSiyao.SetValue(value=privkey.save_pkcs1().decode())

btnShengcheng.Bind(wx.EVT_BUTTON, rsaShengcheng)

def rsaJiami(event):
    textRsaMiwen.SetValue(value='')
    message = textMinwen.GetValue()
    pubkey =rsa.PublicKey.load_pkcs1(textGongyao.GetValue().encode())
    crypto_email_text = rsa.encrypt(message.encode(), pubkey)
    textRsaMiwen.SetValue(value=base64.b64encode(crypto_email_text))

btnRsaJiami.Bind(wx.EVT_BUTTON, rsaJiami)

def rsaJiemi(event):
    textRsaMinwen.SetValue(value='')
    crypto_email_text=base64.b64decode(textRsaMiwen.GetValue())
    privkey=rsa.PrivateKey.load_pkcs1(textSiyao.GetValue().encode())
    message = rsa.decrypt(crypto_email_text, privkey).decode()
    textRsaMinwen.SetValue(value=message)

btnRsaJiemi.Bind(wx.EVT_BUTTON, rsaJiemi)

def rsaQianming(event):
    message = textMinwen.GetValue()
    privkey = rsa.PrivateKey.load_pkcs1(textSiyao.GetValue().encode())
    crypto_email_text = (rsa.sign(message.encode(), privkey, 'SHA-1'))
    textQianming.SetValue(value=base64.b64encode(crypto_email_text))

btnRsaQianming.Bind(wx.EVT_BUTTON, rsaQianming)

def rsaYanzheng(event):
    try:
        message=textMinwen.GetValue()
        crypto_email_text=base64.b64decode(textQianming.GetValue())
        pubkey=rsa.PublicKey.load_pkcs1(textGongyao.GetValue().encode())
        rsa.verify(message.encode(), crypto_email_text, pubkey)

        dlg = wx.MessageDialog(None, u"成功", u"签名验证")
        if dlg.ShowModal() == wx.ID_YES:
            self.Close(True)
        dlg.Destroy()
    except BaseException:
        dlg = wx.MessageDialog(None, u"失败", u"签名验证")
        if dlg.ShowModal() == wx.ID_YES:
            self.Close(True)
        dlg.Destroy()

btnRsaYanzheng.Bind(wx.EVT_BUTTON, rsaYanzheng)
window.Show(True)
app.MainLoop()

你可能感兴趣的:(2019-06-11)