选择当前活动的execel单元格
打开MD5加密工具---因为不是美工,做工比较粗糙,请见谅,但是很实用!
选择文件
选择要加密的列
加密成功后结果如下:
代码如下:
from tkinter.filedialog import * from tkinter import * import openpyxl import hashlib info ={'path':[]} #字体样式 _font = ['Hack', 20, 'bold'] _font_zi = ['Hack', 15, 'bold'] # 每一个sheet固定的列 def get_max_row(sheet): i=sheet.max_row real_max_row = 0 while i > 0: row_dict = {i.value for i in sheet[i]} if row_dict == {None}: i = i-1 else: real_max_row = i break return real_max_row def make_app(): app = Tk() Label(app,text='MD5加密工具',font=_font).pack() Label(app, text='文件名_选择加密的列,点击加密!', font=_font_zi).pack() Label(app,name="f_name", text='暂无文件', font=_font).pack() #fill=BOTH 左右两边 expand=True 上下两边 Listbox(app,name='listbox',bg='#f2f2f2').pack(fill=BOTH,expand=True) Button(app,name='open', text='打开excel文件',command=ui_getdata,font=_font_zi,width=1).pack(fill=BOTH,expand=True) Button(app,name='compress_min',text='MD5加密小写',command=md5_min_T,font=_font_zi).pack(fill=BOTH,expand=True) Button(app,name='compress_max',text='MD5加密大写',command=md5_max_T,font=_font_zi).pack(fill=BOTH,expand=True) #初始框大小 app.geometry('400x800') return app #ui界面交互 tou_sheet=[] def ui_getdata(): f_name = askopenfilename() #赋值--路径添加到数据字典中 info['path']=f_name if f_name: app.children['f_name']['text'] = f_name.split("/")[-1] #得到文件 again_get_cloum(f_name) def again_get_cloum(f_name): wookbook = openpyxl.load_workbook(f_name) book_sheet = wookbook.active list_box = app.children['listbox'] # 缓存清空数据 list_box.delete(0, END) tou_sheet.clear() for yy in range(1, book_sheet.max_column + 1): print(str(book_sheet.cell(1, yy).value)) tou_sheet.append(book_sheet.cell(1, yy).value) list_box.insert(END, book_sheet.cell(1, yy).value) #加密功能 def md5_min_T(): #重新得到最新的列 # 得到文件 f_name_s = info['path'] if f_name_s: wookbook = openpyxl.load_workbook(f_name_s) book_sheet = wookbook.active listbox = app.children['listbox'] colum = listbox.get(ACTIVE) cloum_num=tou_sheet.index(colum)+1 book_sheet.insert_cols(cloum_num+1, 1) #循环行 for x in range(2, get_max_row(book_sheet) + 1): if x == 2: book_sheet.cell(1, cloum_num + 1, book_sheet.cell(1, cloum_num).value+"_小写_加密成功") book_sheet.cell(x, cloum_num+1, md5_min(book_sheet.cell(x, cloum_num).value)) wookbook.save(f_name_s) # 得到文件 again_get_cloum(info['path']) def md5_max_T(): #重新得到最新的列 # 得到文件 f_name_s = info['path'] if f_name_s: wookbook = openpyxl.load_workbook(f_name_s) book_sheet = wookbook.active listbox = app.children['listbox'] colum = listbox.get(ACTIVE) cloum_num=tou_sheet.index(colum)+1 book_sheet.insert_cols(cloum_num+1, 1) #循环行 for x in range(2, book_sheet.max_row + 1): if x == 2: book_sheet.cell(1, cloum_num + 1, book_sheet.cell(1, cloum_num).value+"_大写_加密成功") book_sheet.cell(x, cloum_num+1, md5_max(book_sheet.cell(x, cloum_num).value)) wookbook.save(f_name_s) # 得到文件 again_get_cloum(info['path']) def md5_max(s): return hashlib.md5(s.encode()).hexdigest().upper() def md5_min(s): return hashlib.md5(s.encode()).hexdigest() #主显示框 if __name__=='__main__': app = make_app() app.mainloop()
同理
SM3加密工具代码如下:
from tkinter.filedialog import * from tkinter import * import openpyxl from math import ceil info ={'path':[]} #字体样式 _font = ['Hack', 20, 'bold'] _font_zi = ['Hack', 15, 'bold'] def make_app(): app = Tk() Label(app,text='SM3加密工具',font=_font).pack() Label(app, text='文件名_选择加密的列,点击加密!', font=_font_zi).pack() Label(app,name="f_name", text='暂无文件', font=_font).pack() #fill=BOTH 左右两边 expand=True 上下两边 Listbox(app,name='listbox',bg='#f2f2f2').pack(fill=BOTH,expand=True) Button(app,name='open', text='打开excel文件',command=ui_getdata,font=_font_zi,width=1).pack(fill=BOTH,expand=True) Button(app,name='compress_min',text='SM3加密小写',command=sm3_min_T,font=_font_zi).pack(fill=BOTH,expand=True) Button(app,name='compress_max',text='SM3加密大写',command=sm3_max_T,font=_font_zi).pack(fill=BOTH,expand=True) #初始框大小 app.geometry('400x800') return app #ui界面交互 tou_sheet=[] def ui_getdata(): # ui_change('压缩') f_name = askopenfilename() #赋值--路径添加到数据字典中 info['path']=f_name if f_name: app.children['f_name']['text'] = f_name.split("/")[-1] #得到文件 again_get_cloum(f_name) def again_get_cloum(f_name): wookbook = openpyxl.load_workbook(f_name) book_sheet = wookbook.active list_box = app.children['listbox'] # 缓存清空数据 list_box.delete(0, END) tou_sheet.clear() for yy in range(1, book_sheet.max_column + 1): print(str(book_sheet.cell(1, yy).value)) tou_sheet.append(book_sheet.cell(1, yy).value) list_box.insert(END, book_sheet.cell(1, yy).value) #加密功能 def sm3_min_T(): #重新得到最新的列 # 得到文件 f_name_s = info['path'] if f_name_s: wookbook = openpyxl.load_workbook(f_name_s) book_sheet = wookbook.active listbox = app.children['listbox'] colum = listbox.get(ACTIVE) cloum_num=tou_sheet.index(colum)+1 book_sheet.insert_cols(cloum_num+1, 1) #循环行 for x in range(2, book_sheet.max_row + 1): if x == 2: book_sheet.cell(1, cloum_num + 1, book_sheet.cell(1, cloum_num).value+"_小写_加密成功") book_sheet.cell(x, cloum_num+1, sm3_min(book_sheet.cell(x, cloum_num).value)) wookbook.save(f_name_s) # 得到文件 again_get_cloum(info['path']) def sm3_max_T(): #重新得到最新的列 # 得到文件 f_name_s = info['path'] if f_name_s: wookbook = openpyxl.load_workbook(f_name_s) book_sheet = wookbook.active listbox = app.children['listbox'] colum = listbox.get(ACTIVE) cloum_num=tou_sheet.index(colum)+1 book_sheet.insert_cols(cloum_num+1, 1) #循环行 for x in range(2, book_sheet.max_row + 1): if x == 2: book_sheet.cell(1, cloum_num + 1, book_sheet.cell(1, cloum_num).value+"_大写_加密成功") book_sheet.cell(x, cloum_num+1, sm3_max(book_sheet.cell(x, cloum_num).value)) wookbook.save(f_name_s) # 得到文件 again_get_cloum(info['path']) IV="7380166f 4914b2b9 172442d7 da8a0600 a96f30bc 163138aa e38dee4d b0fb0e4e" IV = int(IV.replace(" ", ""), 16) a = [] for i in range(0, 8): a.append(0) a[i] = (IV >> ((7 - i) * 32)) & 0xFFFFFFFF IV = a def out_hex(list1): for i in list1: print("%08x" % i) print("\n") def rotate_left(a, k): k = k % 32 return ((a << k) & 0xFFFFFFFF) | ((a & 0xFFFFFFFF) >> (32 - k)) T_j = [] for i in range(0, 16): T_j.append(0) T_j[i] = 0x79cc4519 for i in range(16, 64): T_j.append(0) T_j[i] = 0x7a879d8a def FF_j(X, Y, Z, j): if 0 <= j and j < 16: ret = X ^ Y ^ Z elif 16 <= j and j < 64: ret = (X & Y) | (X & Z) | (Y & Z) return ret def GG_j(X, Y, Z, j): if 0 <= j and j < 16: ret = X ^ Y ^ Z elif 16 <= j and j < 64: #ret = (X | Y) & ((2 ** 32 - 1 - X) | Z) ret = (X & Y) | ((~ X) & Z) return ret def P_0(X): return X ^ (rotate_left(X, 9)) ^ (rotate_left(X, 17)) def P_1(X): return X ^ (rotate_left(X, 15)) ^ (rotate_left(X, 23)) def CF(V_i, B_i): W = [] for i in range(16): weight = 0x1000000 data = 0 for k in range(i*4,(i+1)*4): data = data + B_i[k]*weight weight = int(weight/0x100) W.append(data) for j in range(16, 68): W.append(0) W[j] = P_1(W[j-16] ^ W[j-9] ^ (rotate_left(W[j-3], 15))) ^ (rotate_left(W[j-13], 7)) ^ W[j-6] str1 = "%08x" % W[j] W_1 = [] for j in range(0, 64): W_1.append(0) W_1[j] = W[j] ^ W[j+4] str1 = "%08x" % W_1[j] A, B, C, D, E, F, G, H = V_i """ print "00", out_hex([A, B, C, D, E, F, G, H]) """ for j in range(0, 64): SS1 = rotate_left(((rotate_left(A, 12)) + E + (rotate_left(T_j[j], j))) & 0xFFFFFFFF, 7) SS2 = SS1 ^ (rotate_left(A, 12)) TT1 = (FF_j(A, B, C, j) + D + SS2 + W_1[j]) & 0xFFFFFFFF TT2 = (GG_j(E, F, G, j) + H + SS1 + W[j]) & 0xFFFFFFFF D = C C = rotate_left(B, 9) B = A A = TT1 H = G G = rotate_left(F, 19) F = E E = P_0(TT2) A = A & 0xFFFFFFFF B = B & 0xFFFFFFFF C = C & 0xFFFFFFFF D = D & 0xFFFFFFFF E = E & 0xFFFFFFFF F = F & 0xFFFFFFFF G = G & 0xFFFFFFFF H = H & 0xFFFFFFFF """ str1 = "%02d" % j if str1[0] == "0": str1 = ' ' + str1[1:] print str1, out_hex([A, B, C, D, E, F, G, H]) """ V_i_1 = [] V_i_1.append(A ^ V_i[0]) V_i_1.append(B ^ V_i[1]) V_i_1.append(C ^ V_i[2]) V_i_1.append(D ^ V_i[3]) V_i_1.append(E ^ V_i[4]) V_i_1.append(F ^ V_i[5]) V_i_1.append(G ^ V_i[6]) V_i_1.append(H ^ V_i[7]) return V_i_1 def hash_msg(msg): # print(msg) len1 = len(msg) reserve1 = len1 % 64 msg.append(0x80) reserve1 = reserve1 + 1 # 56-64, add 64 byte range_end = 56 if reserve1 > range_end: range_end = range_end + 64 for i in range(reserve1, range_end): msg.append(0x00) bit_length = (len1) * 8 bit_length_str = [bit_length % 0x100] for i in range(7): bit_length = int(bit_length / 0x100) bit_length_str.append(bit_length % 0x100) for i in range(8): msg.append(bit_length_str[7-i]) # print(msg) group_count = round(len(msg) / 64) B = [] for i in range(0, group_count): B.append(msg[i*64:(i+1)*64]) V = [] V.append(IV) for i in range(0, group_count): V.append(CF(V[i], B[i])) y = V[i+1] result = "" for i in y: result = '%s%08x' % (result, i) return result def str2byte(msg): # 字符串转换成byte数组 ml = len(msg) msg_byte = [] msg_bytearray = msg.encode('utf-8') for i in range(ml): msg_byte.append(msg_bytearray[i]) return msg_byte def byte2str(msg): # byte数组转字符串 ml = len(msg) str1 = b"" for i in range(ml): str1 += b'%c' % msg[i] return str1.decode('utf-8') def hex2byte(msg): # 16进制字符串转换成byte数组 ml = len(msg) if ml % 2 != 0: msg = '0'+ msg ml = int(len(msg)/2) msg_byte = [] for i in range(ml): msg_byte.append(int(msg[i*2:i*2+2],16)) return msg_byte def byte2hex(msg): # byte数组转换成16进制字符串 ml = len(msg) hexstr = "" for i in range(ml): hexstr = hexstr + ('%02x'% msg[i]) return hexstr def Hash_sm3(msg,Hexstr = 0): if(Hexstr): msg_byte = hex2byte(msg) else: msg_byte = str2byte(msg) return hash_msg(msg_byte) def KDF(Z,klen): # Z为16进制表示的比特串(str),klen为密钥长度(单位byte) klen = int(klen) ct = 0x00000001 rcnt = ceil(klen/32) Zin = hex2byte(Z) Ha = "" for i in range(rcnt): msg = Zin + hex2byte('%08x'% ct) # print(msg) Ha = Ha + hash_msg(msg) # print(Ha) ct += 1 return Ha[0: klen * 2] def sm3_max(s): return Hash_sm3(s).upper() def sm3_min(s): return Hash_sm3(s) #主显示框 if __name__=='__main__': app = make_app() app.mainloop()
希望对你有帮助