1、申 请 I D:dkymore
2、个人邮箱:dkymore@qq.com
3、原创程序:加强版BASE64处理
起因
在CTF比赛和多款APP逆向中发现了自定义码表的BASE64 奈何网络上没有找到方便的高自定义的BASE64编码程序 百度搜到唯一一个可以控制码表的程序竟然程序有BUG 无法修改填充字符 处于方便的目的 使用python自行实现了BASE64
功能
base64PRO
============================
支持自定义码表 自定义填充字符 自定义字符集
脚本分为两个部分
- base64PRO.py 核心源码
- base64PROGUI.py Tk架构实现的简易GUI
使用python import base64PRO 即可在python脚本中使用
base64PRO
-
init
- alphabet : 编码表
- fillChar : 填充字符
-
b64_encode
- base64_str :要编码的字符串
- encoding :编码方式 默认UTF-8
- fill :是否填充填充字符 默认 是
-
b64_decode
- base64_str : 要解码的字符串
- encoding :编码方式 默认UTF-8
- fill :是否填充填充字符 默认 是
base64PRO(f"{码表}","=").b64_decode("MTIz","utf-8",False)
GUI
使用pyinstaller打包
输入 base64_str 后点击encode即可编码 ,其他同理
源码
-
base64PRO
class base64PRO:
def __init__(self,alphabet:str=None,fillChar:str=None):
"""
base64PRO by dkymore
============================
支持自定义码表 自定义填充字符 自定义字符集
"""
if alphabet == None or alphabet == "":
#def default alphabet
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
if len(alphabet) != 64:
raise RuntimeError('base64 Aalphabet len should be 64!')
self.alphabet = alphabet
if fillChar == None or fillChar == "":
#def default fillingChar
fillChar = "="
if len(fillChar) !=1:
raise RuntimeError('FillChar should be 1 char!')
if fillChar in alphabet:
raise RuntimeError('FillChar should not in alphabet!')
self.fillChar = fillChar
def check_base64_str(self,base64d_str:str=None,fill:bool=True) -> str:
if base64d_str == None:
raise RuntimeError('pls input base64_str')
if len(base64d_str) % 4 and fill:
# check is %4 and try fill with fillChar
for i in range(len(base64d_str)%4):
base64d_str += self.fillChar
for char in base64d_str:
if char not in self.alphabet+self.fillChar:
raise RuntimeError('you input some wrong char which cant find in alphabet!')
return base64d_str
def b64_encode(self,base64_str:str=None,encoding:str='utf-8',fill:bool=True) -> str:
"""
b64_encode
============================
输入需要编码的字符串和编码方式
"""
if base64_str == None or base64_str == "":
raise RuntimeError("you should input base64_str")
if encoding == "":
encoding = 'utf-8'
tmp_str = str()
ret = str()
bit_base64_str = base64_str.encode(encoding)
remain = len( base64_str ) % 3
remain_str = str()
for char in bit_base64_str:
b_char = (bin(char)[2:])
b_char = '0'*(8-len(b_char)) + b_char
tmp_str += b_char
for i in range(len(tmp_str)//6):
temp_nub = int(tmp_str[i*6:6*(i+1)],2)
ret += self.alphabet[temp_nub]
if not fill:
return ret
if remain==2:
remain_str = tmp_str[-4:] + '0'*2
temp_nub = int(remain_str,2)
ret += self.alphabet[temp_nub] + self.fillChar
elif remain==1:
remain_str = tmp_str[-2:] + '0'*4
temp_nub = int(remain_str,2)
ret += self.alphabet[temp_nub] + self.fillChar*2
return ret
def b64_decode(self,base64d_str:str=None,encoding:str='utf-8',fill:bool=True) -> str:
"""
b64_decode
============================
输入需要解码的字符串和编码方式
"""
if encoding == "":
encoding = 'utf-8'
tmp_str = str()
b_char = str()
ret = []
base64d_str = self.check_base64_str(base64d_str=base64d_str,fill=fill)
base64d_str = base64d_str.replace(self.fillChar,'')
for i in range(len(base64d_str)):
b_char = bin(self.alphabet.index(base64d_str[i]))
tmp_str += '{:06}'.format(int(str(b_char).replace('0b', '')))
for i in range(len(tmp_str)//8):
ret.append(int(tmp_str[i*8:8*(i+1)],2))
try:
tmp_str = bytes(ret).decode(encoding)
except:
raise RuntimeError(f"bytes cant format to {encoding}")
return tmp_str
-
base64PROGUI
import tkinter as tk
import base64PRO
#func
def base64_encode():
output.delete(1.0,tk.END)
if not base64_str.get():
output.insert('insert', "you have not input base64_str")
return
try:
output.insert('insert', base64PRO.base64PRO(alphabet=alphabet.get(),fillChar=fillChar.get()).b64_encode(base64_str = base64_str.get() , encoding= encoding.get(),fill=bool(fill.get())))
except RuntimeError as e:
output.insert('insert', e)
return
def base64_decode():
output.delete(1.0,tk.END)
if not base64_str.get():
output.insert('insert', "you have not input base64_str")
return
try:
output.insert('insert', base64PRO.base64PRO(alphabet=alphabet.get(),fillChar=fillChar.get()).b64_decode(base64d_str = base64_str.get() , encoding= encoding.get(),fill=bool(fill.get())))
except RuntimeError as e:
output.insert('insert', e)
return
#windows
window = tk.Tk()
window.title('base64PRO by dkymore')
window.geometry('700x200')
#input
alphabet_text = tk.Label(window, text="alphabet")
alphabet_text.grid(column=0, row=0)
alphabet = tk.Entry(window, show=None, font=('Arial', 14),width=40)
alphabet.grid(column=1, row=0)
fillChar_text = tk.Label(window, text="fillChar")
fillChar_text.grid(column=0, row=1)
fillChar = tk.Entry(window, show=None, font=('Arial', 14),width=40)
fillChar.grid(column=1, row=1)
base64_str_text = tk.Label(window, text="base64_str/base64d")
base64_str_text.grid(column=0, row=2)
base64_str = tk.Entry(window, show=None, font=('Arial', 14),width=40)
base64_str.grid(column=1, row=2)
encoding_text = tk.Label(window, text="encoding")
encoding_text.grid(column=0, row=3)
encoding = tk.Entry(window, show=None, font=('Arial', 14),width=40)
encoding.grid(column=1, row=3)
fill = tk.IntVar()
fill_box =tk.Checkbutton(window, text = "fill?", variable = fill)
fill_box.grid(column=0, row=4)
#output
output = tk.Text(window, height=4)
output.grid(column=1, row=4)
#button
b_encode = tk.Button(window, text='encode', font=('Arial', 12), width=10, height=1, command=base64_encode)
b_encode.grid(column=0, row=5)
b_decode = tk.Button(window, text='decode', font=('Arial', 12), width=10, height=1, command=base64_decode)
b_decode.grid(column=1, row=5)
#mainloop
window.mainloop()
成品
https://wdljt.cowtransfer.com/s/22aad1c9ace14e
|