Last active
January 8, 2018 05:54
-
-
Save hzshang/046c5a38cd8a5a8c8142b67e97ea20c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | |
def base64_encode64(str): | |
add=3-len(str)%3 | |
if(add!=3): | |
str+=add*'\x00' | |
bits="".join(format(x,'08b') for x in bytearray(str)) | |
list=[] | |
for i in xrange(0,len(bits),6): | |
list.append(int(bits[i:i+6],2)) | |
ret="".join(map(lambda i:table[i],list)) | |
if(add!=3): | |
ret=ret[:-add]+'='*add | |
return ret | |
def base64_decode64(str): | |
bits="".join(format(x,'06b') for x in bytearray(map(lambda i:table.index(i),str.replace('=','')))) | |
list=[] | |
for i in xrange(0,len(bits),8): | |
list.append(chr(int(bits[i:i+8],2))) | |
return "".join(list).replace('\x00','') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment