import PySimpleGUI as sg from padding_oracle_demo import po_main import time # 引入time模块 import AES_CBC as CBC import AES_CFB as CFB import AES_CTR as CTR import AES_ECB as ECB import AES_OFB as OFB import time # 密钥设置,请设置为16.24.32 ENCKEY = '' user_get_enc = '' # 定义窗口 def PO_attack(): layout = [ [sg.T('密文填塞攻击')], [sg.T('正常用户:')], [sg.Text('请输入需要加密的明文 :'), sg.InputText('', size=(32, 1), key='-user_mw-')], [sg.Text('请输入使用的密钥 :'), sg.InputText('', size=(32, 1), key='-user_key-')], [sg.Text('请输入使用的IV :'), sg.InputText('', size=(32, 1), key='-user_iv-')], [sg.Button('开始加密', key='-user_start-')], [sg.Text('加密密文:')], [sg.Output(size=(80, 2), key='-user_enc-', echo_stdout_stderr=True)], # [sg.Output(size=(100, 5))], [sg.Text('')], [sg.T('攻击者:')], [sg.Text('请输入需要攻击的密文 :')], [sg.Multiline('', size=(80, 2), key='-attacker_enc-')], [sg.Text('请输入需要攻击的IV :'), sg.InputText('', size=(32, 1), key='-attacker_iv-')], [sg.Text('请输入需要加密的明文 :'), sg.InputText('', size=(32, 1), key='-attacker_mw-')], [sg.Button('开始攻击', key='-attacker_start-')], [sg.Output(size=(80, 3), key='-attacker_output-', echo_stdout_stderr=True)], [sg.Text('')] ] return sg.Window('密文填塞攻击攻击', layout) class GUI: # 主窗口 def __init__(self): sg.theme('DefaultNoMoreNagging') list1 = ['ECB模式', 'CBC模式', 'CFB模式', 'OFB模式', 'CTR模式'] list2 = ['加密', '解密'] layout = [ [sg.Text('请选择加密/解密'), sg.Combo(list2, key='-crypt-', size=5, enable_events=True)], [sg.Text('请输入密钥'), sg.InputText('', key='-key_text-')], [sg.FileBrowse('选择文件', target='-FilePath-', file_types=(('ALL Files', '*.txt'),)), sg.InputText('', size=(100, 1), key='-FilePath-')], [sg.Button('开始执行加密/解密', size=(50, 2), key='-start-')], [sg.Text('采用ECB模式'), sg.Text('', size=5, key='-ECBkey-', border_width=1, relief='ridge'), sg.Text('的结果是:'), sg.InputText('', key='-ECB-'), sg.T('用时:'), sg.InputText('', key='ECBtime', )], [sg.Text('采用CBC模式'), sg.Text('', size=5, border_width=1, relief='ridge', key='-CBCkey-'), sg.Text('的结果是:'), sg.InputText('', key='-CBC-'), sg.T('用时:'), sg.InputText('', key='CBCtime')], [sg.Text('采用CFB模式'), sg.Text('', size=5, border_width=1, relief='ridge', key='-CFBkey-'), sg.Text('的结果是:'), sg.InputText('', key='-CFB-'), sg.T('用时:'), sg.InputText('', key='CFBtime')], [sg.Text('采用OFB模式'), sg.Text('', size=5, border_width=1, relief='ridge', key='-OFBkey-'), sg.Text('的结果是:'), sg.InputText('', key='-OFB-'), sg.T('用时:'), sg.InputText('', key='OFBtime')], [sg.Text('采用CTR模式'), sg.Text('', size=5, border_width=1, relief='ridge', key='-CTRkey-'), sg.Text('的结果是:'), sg.InputText('', key='-CTR-'), sg.T('用时:'), sg.InputText('', key='CTRtime')], [sg.Button('Padding_Oracle_Attack', size=(50, 1), key='-PO_Attack-')] ] self.window = sg.Window('AES', layout, size=(1200, 500), element_padding=(5, 10), font='宋体', grab_anywhere=True, resizable=True, element_justification='left') # 事件 def run(self): while True: event, values = self.window.read() print(event) if event == None: break elif event == '-crypt-': print('加密模式') self.window['-ECBkey-'].update(values['-crypt-']) self.window['-CBCkey-'].update(values['-crypt-']) self.window['-CFBkey-'].update(values['-crypt-']) self.window['-OFBkey-'].update(values['-crypt-']) self.window['-CTRkey-'].update(values['-crypt-']) print('111') elif event == '-start-': print("开始执行") with open(values['-FilePath-'], 'r', encoding='utf-8') as fp: M = fp.read() print(M) # 加密 if values['-crypt-'] == '加密': # CBC模式 cbc = CBC.aestest(values['-key_text-']) T1_CBC = time.time() for i in range(1000): C_CBC = cbc.encrypt(M) T2_CBC = time.time() T_CBC = T2_CBC - T1_CBC # CBC加密时间 print(T_CBC) # print(C_CBC) self.window['-CBC-'].update(C_CBC) self.window['CBCtime'].update(T_CBC) # CFB模式 cfb = CFB.aestest(values['-key_text-']) T1_CFB = time.time() for i in range(1000): C_CFB = cfb.encrypt(M) T2_CFB = time.time() T_CFB = T2_CFB - T1_CFB # CFB加密时间 print(T_CFB) self.window['-CFB-'].update(C_CFB) self.window['CFBtime'].update(T_CFB) # CTR模式 ctr = CTR.aestest(values['-key_text-']) T1_CTR = time.time() for i in range(1000): C_CTR = ctr.encrypt(M) T2_CTR = time.time() T_CTR = T2_CTR - T1_CTR # CTR加密时间 print(T_CTR) self.window['-CTR-'].update(C_CTR) self.window['CTRtime'].update(T_CTR) # ECB模式 ecb = ECB.aestest(values['-key_text-']) T1_ECB = time.time() for i in range(1000): C_ECB = ecb.encrypt(M) T2_ECB = time.time() T_ECB = T2_ECB - T1_ECB # ECB加密时间 print(T_ECB) self.window['-ECB-'].update(C_ECB) self.window['ECBtime'].update(T_ECB) # OFB模式 ofb = OFB.aestest(values['-key_text-']) T1_OFB = time.time() for i in range(1000): C_OFB = ofb.encrypt(M) T2_OFB = time.time() T_OFB = T2_OFB - T1_OFB # OFB加密时间 print(T_OFB) self.window['-OFB-'].update(C_OFB) self.window['OFBtime'].update(T_OFB) elif values['-crypt-'] == '解密': # CBC模式 cbc = CBC.aestest(values['-key_text-']) T1_CBC = time.time() for i in range(1000): C_CBC = cbc.decrypt(M) T2_CBC = time.time() T_CBC = T2_CBC - T1_CBC # CBC解密时间 print(T_CBC) self.window['-CBC-'].update(C_CBC) self.window['CBCtime'].update(T_CBC) # CFB模式 cfb = CFB.aestest(values['-key_text-']) T1_CFB = time.time() for i in range(1000): C_CFB = cfb.decrypt(M) T2_CFB = time.time() T_CFB = T2_CFB - T1_CFB # CFB解密时间 print(T_CFB) self.window['-CFB-'].update(C_CFB) self.window['CFBtime'].update(T_CFB) # CTR模式 ctr = CTR.aestest(values['-key_text-']) T1_CTR = time.time() for i in range(1000): C_CTR = ctr.decrypt(M) T2_CTR = time.time() T_CTR = T2_CTR - T1_CTR # CTR解密时间 print(T_CTR) self.window['-CTR-'].update(C_CTR) self.window['CTRtime'].update(T_CTR) # ECB模式 ecb = ECB.aestest(values['-key_text-']) T1_ECB = time.time() for i in range(1000): C_ECB = ecb.decrypt(M) T2_ECB = time.time() T_ECB = T2_ECB - T1_ECB # ECB解密时间 print(T_ECB) self.window['-ECB-'].update(C_ECB) self.window['ECBtime'].update(T_ECB) # OFB模式 ofb = OFB.aestest(values['-key_text-']) T1_OFB = time.time() for i in range(1000): C_OFB = ofb.decrypt(M) T2_OFB = time.time() T_OFB = T2_OFB - T1_OFB # OFB解密时间 print(T_OFB) self.window['-OFB-'].update(C_OFB) self.window['OFBtime'].update(T_OFB) elif event == '-key_text-': print(values['-key_text-']) # 打印出密钥 elif event == '-PO_Attack-': print('Padding模式') win1 = PO_attack() while True: event1, value1 = win1.read() if event1 == '-user_start-': print(value1['-user_mw-']) user_mw = value1['-user_mw-'] user_iv = value1['-user_iv-'] ENCKEY = value1['-user_key-'].encode('utf-8') user_enc = po_main(user_mw, user_iv, ENCKEY) print(user_enc) user_get_enc = user_enc # 给攻击者使用,因为输出的是字符串包裹的bytes难以转换 win1['-user_enc-'].update(f'正常用户密文: {user_enc}') win1['-attacker_output-'].update('') if event1 == '-attacker_start-': try: attacker_enc = value1['-attacker_enc-'] print(attacker_enc) print(type(attacker_enc), '\n') except: print('change failed!\n') attacker_enc = user_get_enc attacker_iv = value1['-attacker_iv-'] attacker_mw = value1['-attacker_mw-'] attacker_user_mw, attacker_enc_mw = po_main(None, attacker_iv, ENCKEY, attacker_enc, attacker_mw) print(attacker_user_mw, attacker_enc_mw) win1['-attacker_output-'].update(f'正常用户明文: {attacker_user_mw}\n攻击者生成的密文: {attacker_enc_mw}') elif event1 is None: break self.window.close() if __name__ == '__main__': BaseW = GUI() BaseW.run()