import base64
import struct
raw_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
new_table = '0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm+/='
dictionary_decode = str.maketrans(new_table, raw_table) # 创建字符映射关系 用于base64decode
dictionary_encode = dict(zip(dictionary_decode.values(),dictionary_decode.keys())) # 创建一个与上面反向的映射关系用于base64encode
result = '123123' # 123123 原始的数据
result_b64 = base64.b64encode(result.encode()).decode() # MTIzMTIzMTIz base64encode(v) 正常的123123进行base64以后的值
new_result_b64 = result_b64.translate(dictionary_encode) # EP8hEP8hEP8h base64encode(v,table) 换表以后base64以后的值
new_data = new_result_b64.translate(dictionary_decode) # MTIzMTIzMTIz base64encode(v) 变回正常的值
Python通过str.translate实现base64换表,优雅和高效的换表方式
于 2022-05-02 09:57:01 首次发布