base64位加解密专题研究

实现破解自定义加解密的破解方法
 1 import base64
 2 
 3 def custom_base64_encode(data, custom_charset):
 4     encoded_chars = []
 5     for byte in data:
 6         encoded_chars.append(custom_charset[byte >> 2 & 0x3F])
 7         encoded_chars.append(custom_charset[(byte & 0x03) << 4])
 8     return ''.join(encoded_chars)
 9 
10 def custom_base64_decode(data, custom_charset):
11     decoded_chars = []
12     for i in range(0, len(data), 2):
13         char1 = custom_charset.index(data[i])
14         char2 = custom_charset.index(data[i+1])
15         byte = (char1 << 2) | (char2 >> 4)
16         decoded_chars.append(byte)
17     return bytes(decoded_chars)
18 
19 # 自定义base64字符集
20 custom_charset = "123456ertyuytdf456ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+"
21 
22 # 原始数据
23 original_data = b"Hello, World!"
24 
25 # 标准base64编码
26 standard_encoded_data = base64.b64encode(original_data).decode()
27 
28 # 自定义base64编码
29 custom_encoded_data = custom_base64_encode(original_data, custom_charset)
30 
31 # 标准base64解码
32 standard_decoded_data = base64.b64decode(standard_encoded_data)
33 
34 # 自定义base64解码
35 custom_decoded_data = custom_base64_decode(custom_encoded_data, custom_charset)
36 
37 print("原始数据:", original_data)
38 print("标准base64编码:", standard_encoded_data)
39 print("自定义base64编码:", custom_encoded_data)
40 print("标准base64解码:", standard_decoded_data)

 

posted on 2023-07-27 10:16  jiapengchu  阅读(6)  评论(0编辑  收藏  举报

导航