# print(npad) # print(sbin) lst = re.findall(r'.{1,6}', sbin) # print(lst) out = '' for i inrange(0, len(lst) - npad): table_i = int(lst[i], 2) out = out + self.table[table_i] out = out + '=' * npad return out
defdecode(self, data): s = data.replace('=', '') # print(s) sbin = '' for i, d inenumerate(s): talbe_i = self.table.find(d) if -1 == talbe_i: print('error data :%c', d) return
t = bin(talbe_i)[2:] t = '0' * (6 - len(t)) + t sbin = sbin + t
# print(sbin) # print(len(sbin)%8) end = len(sbin) - len(sbin) % 8 sbin = sbin[0:end] lst = re.findall(r'.{1,8}', sbin) # print(lst) out = '' for i, d inenumerate(lst): # print(i,d) ch = chr(int(d, 2)) # print(ch) out = out + ch # print(out) # print(bin(ord(out[len(out)-1]))[2:]) return out
defmain(): s = raw_input("Input:\n") print('加密:') d = Base64().encode(s) print(d) print('解密:') print(Base64().decode(d))