PhieuLang

b64

Sep 14th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. # author: phieulang1993
  2. global charsets
  3. # charsets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  4. charsets = 'aABCDEFGHIJKLMNOPQRSTUVWXYZbcdefghijklmnopqrstuvwxyz0123456789+/'
  5.  
  6. def text2bin(text):
  7.     return ''.join(format(ord(x), 'b').zfill(8) for x in text)
  8.  
  9. def b64_encode(text):
  10.     bin = text2bin(text)
  11.     c = ""
  12.     for x in xrange(0,len(bin),6):
  13.         pos = int(bin[x:x+6],2)
  14.         c += charsets[pos]
  15.     return c
  16. def b64_decode(cipher):
  17.     text = ""
  18.     b = ""
  19.     for c in cipher:
  20.         pos = charsets.index(c)
  21.         b += bin(pos)[2:].zfill(6)
  22.     for x in xrange(0,len(b),8):
  23.         text += chr(int(b[x:x+8],2))
  24.     return text
  25. print b64_encode('phieulang')
  26. print b64_decode('cFhpYWUsXV5n')
Advertisement
Add Comment
Please, Sign In to add comment