Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # author: phieulang1993
- global charsets
- # charsets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
- charsets = 'aABCDEFGHIJKLMNOPQRSTUVWXYZbcdefghijklmnopqrstuvwxyz0123456789+/'
- def text2bin(text):
- return ''.join(format(ord(x), 'b').zfill(8) for x in text)
- def b64_encode(text):
- bin = text2bin(text)
- c = ""
- for x in xrange(0,len(bin),6):
- pos = int(bin[x:x+6],2)
- c += charsets[pos]
- return c
- def b64_decode(cipher):
- text = ""
- b = ""
- for c in cipher:
- pos = charsets.index(c)
- b += bin(pos)[2:].zfill(6)
- for x in xrange(0,len(b),8):
- text += chr(int(b[x:x+8],2))
- return text
- print b64_encode('phieulang')
- print b64_decode('cFhpYWUsXV5n')
Advertisement
Add Comment
Please, Sign In to add comment