base64编码-Python-创新互联

# base64解码
class Decoder(object):
    tables = None 
    def __init__(self):
        self.__generateCodeTable()
    def __generateCodeTable(self):
        """
        解码的码表
        """
        codeTable = {}
        # ord(ss) 输出字符的ascii
        # chr(num) 输出数值对应的字符
        init_char = ord('A') - 1
        for i in range(26):
            init_char = chr(init_char + 1)
            codeTable[init_char] = i
        init_char = ord('a') - 1
        for j in range(26,52):
            init_char = chr(init_char + 1)
            codeTable[init_char] = j
        init_char = ord('0') -1
        for k in range(52,62):
            init_char = chr(init_char + 1)
            codeTable[init_char] = k
        codeTable['+'] = 62
        codeTable['/'] = 63
        self.tables = codeTable
    def __characterTo6Bin(self,msg):
        """
        将每个字符串转化为6位的二进制
        """
        result = ''
        for i in range(len(msg)):
            tep_str = ''
            tmp = bin(self.tables.get(msg[i]))[2:]
            if len(tmp) < 6 :
                num = 6 - len(tmp)
                for j in range(num):
                    tep_str += '0'
            tep_str += tmp
            result += tep_str
        return result
    def decode(self,msg):
        """
        解码base64编码的字符
        """
        index = msg.find('=')
        if index != -1:
            msg = msg[0:index]
        result = []
        str_bin = self.__characterTo6Bin(msg)
        length = len(str_bin)
        index = 0
        while index < length:
            if int(str_bin[index:index+8],2) > 127:
                tmp = str_bin[index:index+24]
                hex_str = str(hex(int(tmp,2)))
                hex_str = hex_str.replace('0x','')
                result.append(str(bytes.fromhex(hex_str),encoding='utf-8'))
                index += 24
            else:
                sub = str_bin[index:index+8]
                result.append(chr(int(sub,2)))
                index += 8
        if result[-1] == 0:
            result = result[0:-1]
        origin = ""
        for key in result:
            origin += key
        return origin.replace('\x00','')
decode = Decoder()
decode.decode('5Lit5Zu9')

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

创新互联建站是一家专注于成都网站设计、做网站与策划设计,保靖网站建设哪家好?创新互联建站做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:保靖等地区。保靖做网站价格咨询:13518219792
网站栏目:base64编码-Python-创新互联
链接URL:http://csdahua.cn/article/dhjooe.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流