1. Base64的用法
import base64encodestr = base64.b64encode('abcr34r344r'.encode('utf-8'))
print(encodestr) encodestr = base64.b64encode('abcr34r344r'.encode('utf-8'))
print(str(encodestr, 'utf-8'))
以“==”或者“=”结尾的加密字符串,往往就是经过Base64加密得来的。如下代码:
from base64 import b64decodecode = ['d3d3Lmh1YXdlaS5jb20=', 'd3d3Lmp1ZWppbi5pbQ==']
for c in code:string = b64decode(c).decode('utf8')print(string)
执行结果如下:
www.huawei.com
www.juejin.im
基于Base64实现新的编码规则
Base64编码和解码时都是将原本的8位二进制转成6位的二进制数,如果我们改动位数,将其设置为5位或者4位,就可以实现新的编码规则。代码如下:
class Custom64:comparison = {'0': 'A', '1': 'B', '2': 'C', '3': 'D', '4': 'E','5': 'F', '6': 'G', '7': 'H', '8': 'I', '9': 'J','10': 'K', '11': 'L', '12': 'M', '13': 'N', '14': 'O','15': 'P', '16': 'Q', '17': 'R', '18': 'S', '19': 'T','20': 'U', '21': 'V', '22': 'W', '23': 'X', '24': 'Y','25': 'Z', '26': 'a', '27': 'b', '28': 'c', '29': 'd','30': 'e', '31': 'f', '32': 'g', '33': 'h', '34': 'i','35': 'j', '36': 'k', '37': 'l', '38': 'm', '39': 'n','40': 'o', '41': 'p', '42': 'q', '43': 'r', '44': 's','45': 't', '46': 'u', '47': 'v', '48': 'w', '49': 'x','50': 'y', '51': 'z', '52': '0', '53': '1', '54': '2','55': '3', '56': '4', '57': '5', '58': '6', '59': '7','60': '8', '61': '9', '62': '+', '63': '/', '65': '=',}def encode(self, value: str, threshold: int = 4) -> str:value = ''.join(['0' + bin(ord(t))[2:] for t in value])inputs = self.shift(value, threshold)result = ''for i in inputs:if i == '0' * threshold:encoding = 65else:encoding = 0for key, v in enumerate(i):val = int(v) * pow(2, len(i) - 1 - key)encoding += valafter = self.comparison.get(str(encoding))result += afterreturn resultdef decode(self, value: str, threshold: int, group: int = 8) -> str:"""对传入的字符串解码,得到原字符"""result = []coder = self.str2binary(value, threshold=threshold)bins = self.shift(''.join(coder), group)for i in range(len(bins)):binary = ''.join(bins)[i * group: (i + 1) * group]if binary != '0' * group:result.append(''.join([chr(i) for i in [int(b, 2) for b in binary.split(' ')]]))return ''.join(result)def str2binary(self, value: str, threshold: int = 6) -> list:"""字符串转十进制再转二进制"""result = []values = self.str2decimal(value)for i in values:if i == '65':val = '0' * thresholdelse:val = '{:0{threshold}b}'.format(int(i), threshold=threshold)result.append(val)return result@staticmethoddef shift(value: str, threshold: int, group: int = 24) -> list:"""位数转换"""remainder = len(value) % groupif remainder:padding = '0' * (group - remainder)value += paddingresult = [value[i:i + threshold] for i in range(0, len(value), threshold)]return resultdef str2decimal(self, value: str) -> list:"""使用Base64编码表做对照,取出字符串对应的十进制数"""keys = []for t in value:for k, v in self.comparison.items():if v == t:keys.append(k)return keysif __name__ == '__main__':cus = Custom64()encode_res = cus.encode('async', threshold=5)decode_res = cus.decode(encode_res, threshold=5)print(encode_res)print(decode_res)
执行结果如下:
MFZXSbTD=A
async
注意:threshold的值可以设置成4,5,6等。
encode_res = cus.encode('async', threshold=6)
decode_res = cus.decode(encode_res, threshold=6)