python带界面带偏移的AES 加密工具

'''
需求: 做一个带界面的AES加密解密小工具
1.支持选择加密方式:ECB,CBC,CTR,CFB,OFB
2.支持加密, 支持选择偏移量iv
3.支持解密, 支持选择便宜量iv
'''
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

import tkinter as tk


# 如果text不足16位的倍数就用空格补足为16位
def add_to_16(text):
    if len(text.encode('utf-8')) % 16:
        add = 16 - (len(text.encode('utf-8')) % 16)
    else:
        add = 0
    text = text + ('\0' * add)
    return text.encode('utf-8')


# 加密函数
def encrypt(text,key,iv):
    key = key.encode('utf-8')
    mode = AES.MODE_CBC
    # iv = b'qqqqqqqqqqqqqqqq'
    text = add_to_16(text)
    cryptos = AES.new(key, mode, iv)
    cipher_text = cryptos.encrypt(text)
    # 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串
    return b2a_hex(cipher_text)


# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text,key,iv):
    key = key.encode('utf-8')
    # iv = b'qqqqqqqqqqqqqqqq'
    mode = AES.MODE_CBC
    cryptos = AES.new(key, mode, iv)
    plain_text = cryptos.decrypt(a2b_hex(text))
    return bytes.decode(plain_text).rstrip('\0')

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.text1 = tk.Label(self,text='输入框:')
        self.text1.pack()
        self.text_input = tk.Entry(self,width=50)
        self.text_input.pack()

        self.text2 = tk.Label(self,text='密码框:')
        self.text2.pack()
        self.key_input = tk.Entry(self)
        self.key_input.pack()

        self.text3 = tk.Label(self,text='偏移框:')
        self.text3.pack()
        self.vi_input = tk.Entry(self)
        self.vi_input.pack()


        self.button_encrypt = tk.Button(self,text="加密")
        self.button_encrypt["command"] = self.AES_encrypt
        self.button_encrypt.pack(side="top")

        self.button_decrypt = tk.Button(self,text="解密")
        self.button_decrypt["command"] = self.AES_decrypt
        self.button_decrypt.pack(side="top")

        self.text4 = tk.Label(self,text='输出框:')
        self.text4.pack()
        self.output = tk.Entry(self,width=50)
        self.output.pack()

    def AES_encrypt(self):  #加密程序
        self.output.select_clear() #先清除输出框
        text_input = self.text_input.get()  #获取输入
        print(text_input)
        key_input = self.key_input.get()    #获取密码
        vi_input = self.vi_input.get()      #获取偏移
        output = encrypt(text_input,key_input,vi_input)
        self.output.insert(0,output)

    def AES_decrypt(self): #解密程序
        self.output.select_clear()  #先清除输出框
        text_input = self.text_input.get()   #获取输入
        key_input = self.key_input.get()     #获取密码
        vi_input = self.vi_input.get()       #获取偏移
        output = decrypt(text_input,key_input,vi_input)
        self.output.insert(0,output)


if __name__ == '__main__':
    test_text = '12345'
    print('加密前:',test_text)
    e = encrypt(test_text,'1234567890123456','1234567890123456')  # 加密(带偏移)
    d = decrypt(e,'1234567890123456','1234567890123456')  # 解密(带偏移)
    # d = decrypt('1332f94e383c7fe06600e0c332e9b99f','1234567890123456','1234567890123456')  # 测试直接解密
    print("加密后:", e)
    print("解密后:", d)

    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()


你可能感兴趣的:(python)