#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.9.5 on Thu Apr 30 15:26:56 2020
#

import wx
import Crypto
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP 
from Crypto.Cipher import AES 
from Crypto.Cipher import DES 
from Crypto import Hash
from Crypto import Random
from base64 import b64encode
from base64 import b64decode

import ast
# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class Home(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Home.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((600, 572))
        self.notebook_1 = wx.Notebook(self, wx.ID_ANY)
        self.Encrypt = wx.Panel(self.notebook_1, wx.ID_ANY)
        self.cbx_al = wx.ComboBox(self.Encrypt, wx.ID_ANY, choices=["RSA", "DES", "AES"], style=wx.CB_DROPDOWN)
        self.txt_pukey_en = wx.TextCtrl(self.Encrypt, wx.ID_ANY, "")
        self.btn_openfile_pu_en = wx.Button(self.Encrypt, wx.ID_ANY, "Open file ...")
        self.txt_plain_en = wx.TextCtrl(self.Encrypt, wx.ID_ANY, "")
        self.btn_openfile_pl_en = wx.Button(self.Encrypt, wx.ID_ANY, "Open file ...")
        self.btn_Encrypt = wx.Button(self.Encrypt, wx.ID_ANY, "Encrypt")
        self.Decrypt = wx.Panel(self.notebook_1, wx.ID_ANY)
        self.cbx_al_copy = wx.ComboBox(self.Decrypt, wx.ID_ANY, choices=["RSA", "DES", "AES"], style=wx.CB_DROPDOWN)
        self.txt_prkey_de = wx.TextCtrl(self.Decrypt, wx.ID_ANY, "")
        self.btn_openfile_pr_de = wx.Button(self.Decrypt, wx.ID_ANY, "Open file ...")
        self.txt_cipher_de = wx.TextCtrl(self.Decrypt, wx.ID_ANY, "")
        self.btn_openfile_ci_de = wx.Button(self.Decrypt, wx.ID_ANY, "Open file ...")
        self.btn_Decrypt = wx.Button(self.Decrypt, wx.ID_ANY, "Decrypt")
        self.Export_key = wx.Panel(self.notebook_1, wx.ID_ANY)
        self.cbx_al_export = wx.ComboBox(self.Export_key, wx.ID_ANY, choices=["RSA", "DES", "AES"], style=wx.CB_DROPDOWN)
        self.cbx_al_len_key = wx.ComboBox(self.Export_key, wx.ID_ANY, choices=["1024 bit", "2048 bit", "4096 bit"], style=wx.CB_DROPDOWN)
        self.btn_Export = wx.Button(self.Export_key, wx.ID_ANY, "Export")
        self.Check_data = wx.Panel(self.notebook_1, wx.ID_ANY)
        self.cbx_al_check = wx.ComboBox(self.Check_data, wx.ID_ANY, choices=["SHA256", "MD5"], style=wx.CB_DROPDOWN)
        self.txt_plain_check1 = wx.TextCtrl(self.Check_data, wx.ID_ANY, "")
        self.btn_openfile_pl_ch1 = wx.Button(self.Check_data, wx.ID_ANY, "Open file ...")
        self.txt_plain_check2 = wx.TextCtrl(self.Check_data, wx.ID_ANY, "")
        self.btn_openfile_pl_ch2 = wx.Button(self.Check_data, wx.ID_ANY, "Open file ...")
        self.btn_Check = wx.Button(self.Check_data, wx.ID_ANY, "Check")
        self.txt_out1 = wx.TextCtrl(self.Check_data, wx.ID_ANY, "", style=wx.TE_MULTILINE)
        self.txt_out2 = wx.TextCtrl(self.Check_data, wx.ID_ANY, "", style=wx.TE_MULTILINE)
        self.txt_result = wx.TextCtrl(self.Check_data, wx.ID_ANY, "", style=wx.TE_CENTRE)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.Click_pu_en, self.btn_openfile_pu_en)
        self.Bind(wx.EVT_BUTTON, self.Click_pl_en, self.btn_openfile_pl_en)
        self.Bind(wx.EVT_BUTTON, self.Click_Encrypt, self.btn_Encrypt)
        self.Bind(wx.EVT_BUTTON, self.Click_pr_de, self.btn_openfile_pr_de)
        self.Bind(wx.EVT_BUTTON, self.Click_ci_de, self.btn_openfile_ci_de)
        self.Bind(wx.EVT_BUTTON, self.Click_Decrypt, self.btn_Decrypt)
        self.Bind(wx.EVT_COMBOBOX, self.cbx_choose_al, self.cbx_al_export)
        self.Bind(wx.EVT_BUTTON, self.Click_Export, self.btn_Export)
        self.Bind(wx.EVT_BUTTON, self.Click_pl1_check, self.btn_openfile_pl_ch1)
        self.Bind(wx.EVT_BUTTON, self.Click_pl2_check, self.btn_openfile_pl_ch2)
        self.Bind(wx.EVT_BUTTON, self.Click_check, self.btn_Check)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Home.__set_properties
        self.SetTitle("Encrypt-Decrypt")
        self.cbx_al.SetMinSize((111, 30))
        self.cbx_al.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_pukey_en.SetMinSize((400, 150))
        self.txt_pukey_en.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_openfile_pu_en.SetMinSize((-1, 30))
        self.btn_openfile_pu_en.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_plain_en.SetMinSize((400, 150))
        self.txt_plain_en.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_openfile_pl_en.SetMinSize((-1, 30))
        self.btn_openfile_pl_en.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_Encrypt.SetMinSize((-1, 30))
        self.btn_Encrypt.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.cbx_al_copy.SetMinSize((111, 30))
        self.cbx_al_copy.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_prkey_de.SetMinSize((400, 150))
        self.txt_prkey_de.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_openfile_pr_de.SetMinSize((-1, 30))
        self.btn_openfile_pr_de.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_cipher_de.SetMinSize((400, 150))
        self.txt_cipher_de.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_openfile_ci_de.SetMinSize((-1, 30))
        self.btn_openfile_ci_de.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_Decrypt.SetMinSize((-1, 30))
        self.btn_Decrypt.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.cbx_al_export.SetMinSize((111, 30))
        self.cbx_al_export.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.cbx_al_len_key.SetMinSize((111, 30))
        self.cbx_al_len_key.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_Export.SetMinSize((80, 30))
        self.btn_Export.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.cbx_al_check.SetMinSize((80, 30))
        self.cbx_al_check.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_plain_check1.SetMinSize((400, 50))
        self.txt_plain_check1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_openfile_pl_ch1.SetMinSize((-1, 30))
        self.btn_openfile_pl_ch1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_plain_check2.SetMinSize((400, 50))
        self.txt_plain_check2.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_openfile_pl_ch2.SetMinSize((-1, 30))
        self.btn_openfile_pl_ch2.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.btn_Check.SetMinSize((-1, 30))
        self.btn_Check.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_out1.SetMinSize((275, 150))
        self.txt_out1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_out2.SetMinSize((275, 150))
        self.txt_out2.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.txt_result.SetMinSize((250, 40))
        self.txt_result.SetFont(wx.Font(15, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Home.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_11 = wx.BoxSizer(wx.VERTICAL)
        sizer_9 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_13 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_12 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_17 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_8 = wx.BoxSizer(wx.VERTICAL)
        sizer_19 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_18 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_16 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_15 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_14 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_10 = wx.BoxSizer(wx.HORIZONTAL)
        label_1 = wx.StaticText(self.Encrypt, wx.ID_ANY, "Encrypt", style=wx.ALIGN_CENTER)
        label_1.SetFont(wx.Font(25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_2.Add(label_1, 0, wx.ALIGN_CENTER, 0)
        label_8 = wx.StaticText(self.Encrypt, wx.ID_ANY, u"Chọn giải thuật mã hóa:")
        label_8.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_10.Add(label_8, 0, wx.ALIGN_CENTER, 0)
        sizer_10.Add(self.cbx_al, 0, wx.ALIGN_CENTER, 0)
        sizer_2.Add(sizer_10, 1, wx.ALIGN_CENTER, 0)
        label_2 = wx.StaticText(self.Encrypt, wx.ID_ANY, "Public key")
        label_2.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_3.Add(label_2, 0, 0, 0)
        sizer_3.Add(self.txt_pukey_en, 0, wx.ALIGN_CENTER, 0)
        sizer_3.Add(self.btn_openfile_pu_en, 0, 0, 0)
        sizer_2.Add(sizer_3, 1, wx.ALIGN_CENTER, 0)
        label_3 = wx.StaticText(self.Encrypt, wx.ID_ANY, "Plaintext  ")
        label_3.SetMinSize((66, 18))
        label_3.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_4.Add(label_3, 0, 0, 0)
        sizer_4.Add(self.txt_plain_en, 0, wx.ALIGN_CENTER, 0)
        sizer_4.Add(self.btn_openfile_pl_en, 0, 0, 0)
        sizer_2.Add(sizer_4, 1, wx.ALIGN_CENTER, 0)
        sizer_2.Add(self.btn_Encrypt, 0, wx.ALIGN_CENTER, 0)
        self.Encrypt.SetSizer(sizer_2)
        label_4 = wx.StaticText(self.Decrypt, wx.ID_ANY, "Decrypt")
        label_4.SetFont(wx.Font(25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_5.Add(label_4, 0, wx.ALIGN_CENTER, 0)
        label_9 = wx.StaticText(self.Decrypt, wx.ID_ANY, u"Chọn giải thuật mã hóa:")
        label_9.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_14.Add(label_9, 0, wx.ALIGN_CENTER, 0)
        sizer_14.Add(self.cbx_al_copy, 0, wx.ALIGN_CENTER, 0)
        sizer_5.Add(sizer_14, 1, wx.ALIGN_CENTER, 0)
        label_5 = wx.StaticText(self.Decrypt, wx.ID_ANY, "Private key")
        label_5.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_6.Add(label_5, 0, 0, 0)
        sizer_6.Add(self.txt_prkey_de, 0, wx.ALIGN_CENTER, 0)
        sizer_6.Add(self.btn_openfile_pr_de, 0, 0, 0)
        sizer_5.Add(sizer_6, 1, wx.ALIGN_CENTER, 0)
        label_6 = wx.StaticText(self.Decrypt, wx.ID_ANY, "Ciphertext")
        label_6.SetMinSize((71, 18))
        label_6.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_7.Add(label_6, 0, 0, 0)
        sizer_7.Add(self.txt_cipher_de, 0, wx.ALIGN_CENTER, 0)
        sizer_7.Add(self.btn_openfile_ci_de, 0, 0, 0)
        sizer_5.Add(sizer_7, 1, wx.ALIGN_CENTER, 0)
        sizer_5.Add(self.btn_Decrypt, 0, wx.ALIGN_CENTER, 0)
        self.Decrypt.SetSizer(sizer_5)
        label_7 = wx.StaticText(self.Export_key, wx.ID_ANY, "Export key")
        label_7.SetFont(wx.Font(25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_8.Add(label_7, 0, wx.ALIGN_CENTER, 0)
        label_13 = wx.StaticText(self.Export_key, wx.ID_ANY, u"Chọn giải thuật:")
        label_13.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_15.Add(label_13, 0, wx.ALIGN_CENTER, 0)
        sizer_15.Add(self.cbx_al_export, 0, wx.ALIGN_CENTER, 0)
        sizer_8.Add(sizer_15, 1, wx.ALIGN_CENTER | wx.ALL, 0)
        label_14 = wx.StaticText(self.Export_key, wx.ID_ANY, u"Chọn kích thước key:")
        label_14.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_16.Add(label_14, 0, wx.ALIGN_CENTER, 0)
        sizer_16.Add(self.cbx_al_len_key, 0, wx.ALIGN_CENTER, 0)
        sizer_8.Add(sizer_16, 1, wx.ALIGN_CENTER | wx.ALL, 0)
        sizer_8.Add(self.btn_Export, 0, wx.ALIGN_CENTER | wx.ALL, 0)
        sizer_18.Add((0, 0), 0, 0, 0)
        sizer_8.Add(sizer_18, 1, wx.EXPAND, 0)
        sizer_19.Add((0, 0), 0, 0, 0)
        sizer_8.Add(sizer_19, 1, wx.EXPAND, 0)
        self.Export_key.SetSizer(sizer_8)
        label_10 = wx.StaticText(self.Check_data, wx.ID_ANY, "Check data")
        label_10.SetFont(wx.Font(25, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_11.Add(label_10, 0, wx.ALIGN_CENTER, 0)
        label_15 = wx.StaticText(self.Check_data, wx.ID_ANY, u"Chọn giải thuật hash:")
        label_15.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_17.Add(label_15, 0, wx.ALIGN_CENTER, 0)
        sizer_17.Add(self.cbx_al_check, 0, wx.ALIGN_CENTER, 0)
        sizer_11.Add(sizer_17, 1, wx.ALIGN_CENTER, 0)
        label_11 = wx.StaticText(self.Check_data, wx.ID_ANY, "Plaintext")
        label_11.SetMinSize((67, 18))
        label_11.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_12.Add(label_11, 0, 0, 0)
        sizer_12.Add(self.txt_plain_check1, 0, 0, 0)
        sizer_12.Add(self.btn_openfile_pl_ch1, 0, 0, 0)
        sizer_11.Add(sizer_12, 1, wx.ALIGN_CENTER, 0)
        label_12 = wx.StaticText(self.Check_data, wx.ID_ANY, "Decrypted")
        label_12.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_13.Add(label_12, 0, 0, 0)
        sizer_13.Add(self.txt_plain_check2, 0, 0, 0)
        sizer_13.Add(self.btn_openfile_pl_ch2, 0, 0, 0)
        sizer_11.Add(sizer_13, 1, wx.ALIGN_CENTER, 0)
        sizer_11.Add(self.btn_Check, 0, wx.ALIGN_CENTER, 0)
        sizer_9.Add(self.txt_out1, 0, wx.ALIGN_CENTER, 0)
        sizer_9.Add(self.txt_out2, 0, wx.ALIGN_CENTER, 0)
        sizer_11.Add(sizer_9, 1, wx.ALIGN_CENTER, 0)
        sizer_11.Add(self.txt_result, 0, wx.ALIGN_CENTER, 0)
        self.Check_data.SetSizer(sizer_11)
        self.notebook_1.AddPage(self.Encrypt, "Endcrypt")
        self.notebook_1.AddPage(self.Decrypt, "Decrypt")
        self.notebook_1.AddPage(self.Export_key, "Export key")
        self.notebook_1.AddPage(self.Check_data, "Check data")
        sizer_1.Add(self.notebook_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade
    
    def Click_pu_en(self, event):
        with wx.FileDialog(self, message="Choose a file",wildcard = 'Txt file (*.txt)|*.txt',style=style) as file:
            file.ShowModal()
            path = file.GetPath()
        self.txt_pukey_en.Clear()
        self.txt_pukey_en.AppendText(path)
        event.Skip()

    def Click_pl_en(self, event):  # wxGlade: Home.<event_handler>
        with wx.FileDialog(self, message="Choose a file",wildcard = wildcard,style=style) as file:
            file.ShowModal()
            path = file.GetPath()
        self.txt_plain_en.Clear()
        self.txt_plain_en.AppendText(path)
        event.Skip()

    def Click_pr_de(self, event):  # wxGlade: Home.<event_handler>
        with wx.FileDialog(self, message="Choose a file",wildcard = 'Txt file (*.txt)|*.txt',style=style) as file:
            file.ShowModal()
            path = file.GetPath()
        self.txt_prkey_de.Clear()
        self.txt_prkey_de.AppendText(path)
        event.Skip()

    def Click_ci_de(self, event):  # wxGlade: Home.<event_handler>
        with wx.FileDialog(self, message="Choose a file",wildcard = 'Binary files (*.bin)|*.bin',style=style) as file:
            file.ShowModal()
            path = file.GetPath()
        self.txt_cipher_de.Clear()
        self.txt_cipher_de.AppendText(path)
        event.Skip()

    def Click_pl1_check(self, event):  # wxGlade: Home.<event_handler>
        with wx.FileDialog(self, message="Choose a file",wildcard = wildcard,style=style) as file:
            file.ShowModal()
            path = file.GetPath()
        self.txt_plain_check1.Clear()
        self.txt_plain_check1.AppendText(path)
        event.Skip()

    def Click_pl2_check(self, event):  # wxGlade: Home.<event_handler>
        with wx.FileDialog(self, message="Choose a file",wildcard = wildcard,style=style) as file:
            file.ShowModal()
            path = file.GetPath()
        self.txt_plain_check2.Clear()
        self.txt_plain_check2.AppendText(path)
        event.Skip()

    def Click_Encrypt(self, event):  # wxGlade: Home.<event_handler>
        pathname = self.txt_plain_en.GetValue()
        pukey = self.txt_pukey_en.GetValue()
        try:
            with open(pukey, 'r') as file_pu:
                pukey = file_pu.read()
        except IOError:
            pass

        name = getFilename(pathname)

        if (self.cbx_al.GetValue() == "RSA"):
            if name.find('.'):
                try:
                    data = read_file(pathname)
                    cy = al_RSA()
                    ciphertext = cy.Encrypt(pukey,data)
                except Exception as e:
                    wx.LogError(str(e))
                    return 0      
            else:
                pass
        elif (self.cbx_al.GetValue() == "AES"):
            if name.find('.'):
                try:
                    data = read_file(pathname)
                    cy = al_AES()
                    (ciphertext,IV) = cy.Encrypt(pukey, data)
                except Exception as e:
                    wx.LogError(str(e))
                    return 0
            else:
                pass
        elif (self.cbx_al.GetValue() == "DES"):
            if name.find('.'):
                try:
                    data = read_file(pathname)
                    cy = al_DES()
                    (ciphertext,IV) = cy.Encrypt(pukey, data)
                except Exception as e:
                    wx.LogError(str(e))
                    return 0 
            else:
                pass
        else :
            wx.LogError("Choose algorithm to encrypt")
            return 0
        
        f = open (name[:name.find('.')]+'.bin', 'wb+')
        f.write(ciphertext) #write ciphertext to file
        f.close()
                
        wx.LogMessage('Encrypt successful')
        event.Skip()

    def Click_Decrypt(self, event):  # wxGlade: Home.<event_handler>
        pathname = self.txt_cipher_de.GetValue()
        prkey = self.txt_prkey_de.GetValue()
        try:
            with open(prkey, 'r') as file:
                prkey = file.read()
        except IOError:
            pass

        try:
            with open(pathname, 'rb+') as file:
                data = file.read()
        except IOError:
            pass

        if (self.cbx_al_copy.GetValue() == "RSA"):
            cy = al_RSA()
        elif (self.cbx_al_copy.GetValue() == "AES"):
            cy = al_AES()
        elif (self.cbx_al_copy.GetValue() == "DES"):
            cy = al_DES()
        else:
            wx.LogError("Choose algorithm to decrypt")
            return 0

        try:
            plaintext = cy.Decrypt(prkey,data)
        except Exception as e:
            wx.LogError(str(e))
            return 0
        
        name = getFilename(pathname)
        mime = plaintext[plaintext.find(b'mime:')+5:]
        mime = mime.decode('utf-8')
        plaintext = plaintext[:plaintext.find(b'mime:')]
            
        f = open (name[:name.find('.')]+'_decrypted.'+mime, 'wb+')    
        f.write(plaintext) #write ciphertext to file
        f.close()
        
        wx.LogMessage('Decrypt successful')
        event.Skip()
    
    def Click_Export(self, event):  # wxGlade: Home.<event_handler>
        txtlenkey = self.cbx_al_len_key.GetValue()
        
        if self.cbx_al_export.GetValue() == 'RSA':
            lenkey = int(txtlenkey[:4])
            if lenkey % 1024 != 0:
                wx.LogError('Key length is multiple of 1024')
                return 0
            random_generator = Random.new().read
            key = RSA.generate(lenkey, random_generator) #generate pub and priv key
            pukey = key.publickey() # pub key export for exchange
            
            pu = open ('publickey_' + str(lenkey) + '.txt', 'w')
            pu.write(pukey.export_key().decode('utf-8')) #write publickey to file
            pu.close()

            pr = open ('privatekey_' + str(lenkey) + '.txt', 'w')
            pr.write(key.export_key().decode('utf-8')) #write privatekey to file
            pr.close()
        elif self.cbx_al_export.GetValue() == 'DES':
            IV = Random.new().read(DES.block_size)
            key = Random.get_random_bytes(8)
            key_des = open ('key_des_8.txt', 'w')
            k = 'Key:' + b64encode(key).decode('utf-8') 
            iv = 'IV:' + b64encode(IV).decode('utf-8')
            key_des.write(k+iv) #write publickey to file
            key_des.close()
        elif self.cbx_al_export.GetValue() == 'AES':
            lenkey = int(txtlenkey[:2])
            IV = Random.new().read(AES.block_size)
            key = Random.get_random_bytes(lenkey)
            key_aes = open ('key_aes_' + str(lenkey) + '.txt', 'w')
            k = 'Key:' + b64encode(key).decode('utf-8') 
            iv = 'IV:' + b64encode(IV).decode('utf-8')
            key_aes.write(k+iv) #write publickey to file
            key_aes.close()
        wx.LogMessage('Export key successful')
        event.Skip()
    
    def Click_check(self, event):  # wxGlade: Home.<event_handler>
        pl = self.txt_plain_check1.GetValue()
        de = self.txt_plain_check2.GetValue()
        
        if self.cbx_al_check.GetValue() == 'SHA256':
            try:
                with open(pl, 'rb') as file_pl:
                    data1 = file_pl.read()
                with open(de, 'rb') as file_de:
                    data2 = file_de.read()
                h1 = Hash.SHA256.new(data1)
                h2 = Hash.SHA256.new(data2)
            except Exception:
                wx.LogMessage(str(Exception))

        if self.cbx_al_check.GetValue() == 'MD5':
            try:
                with open(pl, 'rb') as file_pl:
                    data1 = file_pl.read()
                with open(de, 'rb') as file_de:
                    data2 = file_de.read()
                h1 = Hash.MD5.new(data1)
                h2 = Hash.MD5.new(data2)
            except Exception:
                wx.LogMessage(str(Exception)) 
        self.txt_out1.Clear()
        self.txt_out1.AppendText(h1.hexdigest())   
        self.txt_out2.Clear()
        self.txt_out2.AppendText(h2.hexdigest())       
        if h1.hexdigest() == h2.hexdigest():
            self.txt_result.Clear()
            self.txt_result.AppendText('Document integrity')
        else: 
            self.txt_result.Clear()
            self.txt_result.AppendText('Document incomplete')

        event.Skip()
        
    def cbx_choose_al(self, event):  # wxGlade: Home.<event_handler>
        if self.cbx_al_export.GetValue() == 'RSA':
            self.cbx_al_len_key.SetString(0,'1024 bit')
            self.cbx_al_len_key.SetString(1,'2048 bit')
            self.cbx_al_len_key.SetString(2,'4096 bit')
        if self.cbx_al_export.GetValue() == 'DES':
            self.cbx_al_len_key.SetString(0,'8 byte')
            self.cbx_al_len_key.SetString(1,'')
            self.cbx_al_len_key.SetString(2,'')
        elif self.cbx_al_export.GetValue() == 'AES':
            self.cbx_al_len_key.SetString(0,'16 byte')
            self.cbx_al_len_key.SetString(1,'24 byte')
            self.cbx_al_len_key.SetString(2,'32 byte')
        event.Skip()
    
# end of class Home
wildcard = "All files (*.*)|*.*|Txt file (*.txt)|*.txt|Binary files (*.bin)|*.bin|Python files (*.py)|*.py|JPG files (*.jpg)|*.jpg"
style = wx.FD_OPEN|wx.FD_MULTIPLE|wx.FD_CHANGE_DIR

def getFilename(path):
    index = path.rfind('\\')
    return path[index+1:]

def read_file(pathname):
    name = getFilename(pathname)
    mime = name[name.find('.')+1:]
    try:
        with open(pathname, 'rb') as file_pl:
            data = file_pl.read()
            a = 'mime:' + mime
            data += bytes(a,'utf-8')
    except IOError:
        wx.LogError(IOError)
    return data

class al_RSA():
    def Encrypt(self, pukey, plaintext):
        if pukey == '':    
            random_generator = Random.new().read
            key = RSA.generate(1024, random_generator) #generate pub and priv key
            pukey = key.publickey() # pub key export for exchange
            
            pu = open ('publickey.txt', 'w')
            pu.write(pukey.export_key().decode('utf-8')) #write publickey to file
            pu.close()

            pr = open ('privatekey.txt', 'w')
            pr.write(key.export_key().decode('utf-8')) #write privatekey to file
            pr.close()
        else:
            try:
                pukey = RSA.import_key(pukey)
            except Exception as e:
                raise e
        
        try:
            pk = PKCS1_OAEP.new(pukey)
            encrypted = pk.encrypt(plaintext)
        except Exception as e:
            raise e

        return encrypted #byte

    def Decrypt(self, prkey, ciphertext):
        prkey = RSA.import_key(prkey)
        
        pk = PKCS1_OAEP.new(prkey)
        decrypted = pk.decrypt(ciphertext)
        
        return decrypted #byte
        
class al_DES():
    def Encrypt(self, key, plaintext):
        if key == '':
            IV = Random.new().read(DES.block_size)
            key = Random.get_random_bytes(8)
            key_des = open ('key_des_8.txt', 'w')
            k = 'Key:' + b64encode(key).decode('utf-8') 
            iv = 'IV:' + b64encode(IV).decode('utf-8')
            key_des.write(k+iv) #write publickey to file
            key_des.close()
        else: 
            IV = b64decode(key[key.rfind('IV:') + 3 :])
            key = b64decode(key[4:key.rfind('IV:')]) 
        des = DES.new(key, DES.MODE_CFB, IV)
        return (des.encrypt(plaintext),IV)
    def Decrypt(self, key, ciphertext):
        IV = b64decode(key[key.rfind('IV:') + 3 :])
        key = b64decode(key[4:key.rfind('IV:')])
        des = DES.new(key, DES.MODE_CFB, IV)
        return des.decrypt(ciphertext)

class al_AES():
    def Encrypt(self,key, plaintext):
        if key == '':
            IV = Random.new().read(AES.block_size)
            key = Random.get_random_bytes(16)
            key_aes = open ('key_aes_16.txt', 'w')
            k = 'Key:' + b64encode(key).decode('utf-8') 
            iv = 'IV:' + b64encode(IV).decode('utf-8')
            key_aes.write(k+iv) #write publickey to file
            key_aes.close()
        else: 
            IV = b64decode(key[key.rfind('IV:') + 3 :])
            key = b64decode(key[4:key.rfind('IV:')]) 
        aes = AES.new(key, AES.MODE_CFB, IV)
        return (aes.encrypt(plaintext),IV)
        
    def Decrypt(self, key, ciphertext):
        IV = b64decode(key[key.rfind('IV:') + 3 :])
        key = b64decode(key[4:key.rfind('IV:')])
        aes = AES.new(key, AES.MODE_CFB, IV)
        return aes.decrypt(ciphertext)

class MyApp(wx.App):
    def OnInit(self):
        self.En_decrypt = Home(None, wx.ID_ANY, "")
        self.SetTopWindow(self.En_decrypt)
        self.En_decrypt.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()
