| 1 | def encodeBase64(text): |
| 2 | base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 3 | |
| 4 | r = "" #the result |
| 5 | c = 3 - len(text) % 3 #the length of padding |
| 6 | p = "=" * c #the padding |
| 7 | s = text + "\0" * c #the text to encode |
| 8 | |
| 9 | i = 0 |
| 10 | while i < len(s): |
| 11 | if i > 0 and ((i / 3 * 4) % 76) == 0: |
| 12 | r = r + "\r\n" |
| 13 | |
| 14 | n = (ord(s[i]) << 16) + (ord(s[i+1]) << 8 ) + ord(s[i+2]) |
| 15 | |
| 16 | n1 = (n >> 18) & 63 |
| 17 | n2 = (n >> 12) & 63 |
| 18 | n3 = (n >> 6) & 63 |
| 19 | n4 = n & 63 |
| 20 | |
| 21 | r += base64chars[n1] + base64chars[n2] + base64chars[n3] + base64chars[n4] |
| 22 | i += 3 |
| 23 | |
| 24 | return r[0: len(r)-len(p)] + p |
| 25 | |
| 26 | def decodeBase64(text): |
| 27 | base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |