前言:
安全工作中經常會遇到需要轉碼或加密的情況,比較好用的有Burp的Decoder模塊
以及IBM APPSCAN的PowerTools中的編碼/解碼工具。
具體用法都非常簡單。今天就用Python編寫一個有GUI界面的簡單的轉碼器,效果如下圖:
代碼:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Time :2020/2/6 21:01
# @Author :Donvin.li
# @File :encodergui.py
import tkinter as tk
from urllib import parse
import base64
import binascii
from hashlib import md5
import html
'''
基本思路:
1、定義一個encode_type變量用於接收編碼/解碼方法;
2、定義Radiobutton控件用於選擇編碼/解碼方法,Radiobutton控件每次選擇將值傳給encode_type;
3、定義兩個Button按鈕,一個用於編碼選擇函數:mian(),一個用於解碼選擇函數:un_mian();
4、定義一個ENTRY控件,用於接收用戶輸入,傳給具體的編碼函數;
5、定義一個TEXT控件,用於顯示具體編碼函數的輸出結果;
6、定義編碼選擇函數mian(),在該函數中判斷encode_type變量的值以選擇具體的編碼函數,如:encode_type='A'時,執行url_encoding();
7、定義具體的編碼函數,如:url_encoding(),傳入的值從ENTRY控件獲得,輸出的結果傳入到TEXT控件進行顯示;
8、定義解碼選擇函數un_mian(),在該函數中判斷encode_type變量的值以選擇具體的解碼函數,如:encode_type='A'時,執行url_decoding();
9、定義具體的解碼函數,如:url_decoding(),傳入的值從ENTRY控件獲得,輸出的結果傳入到TEXT控件進行顯示;
'''
# 定義GUI主體框架
window=tk.Tk()
window.title('轉碼器')
window.geometry('810x355')
encode_type=tk.StringVar()
# 編碼選擇函數
def mian():
encode_type_value = encode_type.get()
if encode_type_value=='A':
url_encoding()
#l4.config(text='you have selected ' + encode_type_value)
elif encode_type_value=='B':
base64_encoding()
elif encode_type_value=='C':
hex_encoding()
elif encode_type_value=='D':
ascii_encoding()
elif encode_type_value=='E':
md5_encoding()
elif encode_type_value=='F':
unicode_encoding()
elif encode_type_value=='G':
htmlescape()
# URL編碼
def url_encoding():
t2.delete('1.0', 'end') #用於清空TEXT框
string=e1.get()
reslut=parse.quote(string)
#return parse.quote(string)
#t2.insert('insert',reslut)
#l4.config(text='you have selected ' + reslut)
t2.insert(tk.INSERT,reslut)
# BASE64編碼
def base64_encoding():
t2.delete('1.0', 'end')
string=e1.get()
str1=base64.b64encode(string.encode()) #base64.b64encode()傳入的是bytes類型,輸出的也是bytes類型
reslut=str1.decode()
t2.insert(tk.INSERT, reslut)
# HEX 十六進制編碼
def hex_encoding():
t2.delete('1.0', 'end')
string=e1.get()
hex=binascii.b2a_hex(string.encode())
reslut='0x'+hex.decode()
t2.insert(tk.INSERT, reslut)
# Ascii編碼
def ascii_encoding():
t2.delete('1.0', 'end')
string=e1.get()
reslut=list(map(ord,string)) #map(ord,string)函數是將string中的每個字符分別傳給ord函數,轉換成數字編號
t2.insert(tk.INSERT, reslut) #輸出的是列表
# MD5加密
def md5_encoding():
t2.delete('1.0', 'end')
string=e1.get()
reslut=md5(string.encode()).hexdigest()
t2.insert(tk.INSERT, reslut)
# unicode編碼函數
def unicode_encoding(): #傳入的是中文
t2.delete('1.0', 'end')
string=e1.get()
reslut=string.encode('unicode_escape').decode()
t2.insert(tk.INSERT, reslut)
# HTML實體編碼
def htmlescape():
t2.delete('1.0', 'end')
string=e1.get()
reslut=html.escape(string)
t2.insert(tk.INSERT, reslut)
# 解碼選擇函數
def un_mian():
encode_type_value = encode_type.get()
if encode_type_value=='A':
url_decoding()
#l4.config(text='you have selected ' + encode_type_value)
elif encode_type_value=='B':
base64_decoding()
elif encode_type_value=='C':
hex_decoding()
elif encode_type_value=='D':
ascii_decoding()
elif encode_type_value=='E':
md5_decoding()
elif encode_type_value=='F':
unicode_decoding()
elif encode_type_value=='G':
unhtmlescape()
# URL解碼
def url_decoding():
t2.delete('1.0', 'end')
string=e1.get()
reslut=parse.unquote(string)
t2.insert(tk.INSERT, reslut)
# BASE64解碼
def base64_decoding():
t2.delete('1.0', 'end')
string=e1.get()
str1=base64.b64decode(string.encode())
reslut=str1.decode()
t2.insert(tk.INSERT, reslut)
# HEX 十六進制解碼
def hex_decoding():
t2.delete('1.0', 'end')
string=e1.get()
str1=string.replace('0x','') #字符串方法,將0x替換爲空
str2=binascii.a2b_hex(str1.encode())
reslut=str2.decode()
t2.insert(tk.INSERT, reslut)
# Ascii解碼
def ascii_decoding(): #輸入的是列表,例如[116, 101, 115, 116]
t2.delete('1.0','end')
string=e1.get()
l1=eval(string)
str1=list(map(chr,l1))
reslut=''.join(str1) #join方法用''將所有字符連接起來
t2.insert(tk.INSERT, reslut)
# MD5解密
def md5_decoding():
t2.delete('1.0', 'end')
t2.insert(tk.INSERT,'你咋不上天呢')
# unicode解碼函數
def unicode_decoding(): #傳入的是Unicode編碼,如:\u6d4b\u8bd5
t2.delete('1.0', 'end')
string=e1.get()
t2.insert(tk.INSERT, string)
# HTML實體解碼
def unhtmlescape(string):
t2.delete('1.0', 'end')
string=e1.get()
reslut=html.unescape(string)
t2.insert(tk.INSERT, string)
l1=tk.Label(window,text='選擇方式',font=('Arial',10))
l1.place(x=10,y=10)
r1 = tk.Radiobutton(window, text='URL', variable=encode_type, value='A')
r1.place(x=10,y=40)
r2 = tk.Radiobutton(window, text='BASE64', variable=encode_type, value='B')
r2.place(x=100,y=40)
r3 = tk.Radiobutton(window, text='HEX', variable=encode_type, value='C')
r3.place(x=200,y=40)
r4 = tk.Radiobutton(window, text='ASCII', variable=encode_type, value='D')
r4.place(x=300,y=40)
r5 = tk.Radiobutton(window, text='MD5', variable=encode_type, value='E')
r5.place(x=400,y=40)
r6 = tk.Radiobutton(window, text='UNICODE', variable=encode_type, value='F')
r6.place(x=500,y=40)
r7 = tk.Radiobutton(window, text='HTML', variable=encode_type, value='G')
r7.place(x=600,y=40)
b1 = tk.Button(window, text='編碼', width=10,height=1, command=mian)
b1.place(x=10,y=70)
b2 = tk.Button(window, text='解碼', width=10,height=1, command=un_mian)
b2.place(x=100,y=70)
l2=tk.Label(window,text='輸入',font=('Arial',10))
l2.place(x=10,y=110)
# t1=tk.Text(window,width=111,height=8)
# t1.place(x=10,y=140)
# string=t1.get()
e1=tk.Entry(window,width=111,show = None) #顯示成明文形式
e1.place(x=10,y=140)
l3=tk.Label(window,text='輸出',font=('Arial',10))
l3.place(x=10,y=170)
# l4=tk.Label(window, bg='yellow', width=111, text='empty')
# l4.place(x=10,y=300)
t2=tk.Text(window,bg='green',fg='white',width=111, height=6)
# 說明: bg爲背景,fg爲字體顏色,font爲字體,width爲長,height爲高,這裏的長和高是字符的長和高,比如height=2,就是標籤有2個字符這麼高
t2.place(x=10,y=200)
l3=tk.Label(window,text='作者:李東鋒',font=('Arial',10))
l3.place(x=350,y=330)
window.mainloop()
使用:
打包成exe雙擊執行即可:
pyinstaller -F -w encoding.py