Python/ciphers/base64_cipher.py

72 lines
1.6 KiB
Python
Raw Normal View History

def encodeBase64(text):
base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
2019-10-05 05:14:13 +00:00
r = "" # the result
c = 3 - len(text) % 3 # the length of padding
p = "=" * c # the padding
s = text + "\0" * c # the text to encode
i = 0
while i < len(s):
if i > 0 and ((i / 3 * 4) % 76) == 0:
r = r + "\r\n"
2019-10-05 05:14:13 +00:00
n = (ord(s[i]) << 16) + (ord(s[i + 1]) << 8) + ord(s[i + 2])
n1 = (n >> 18) & 63
n2 = (n >> 12) & 63
2019-10-05 05:14:13 +00:00
n3 = (n >> 6) & 63
n4 = n & 63
2019-10-05 05:14:13 +00:00
r += base64chars[n1] + base64chars[n2] + base64chars[n3] + base64chars[n4]
i += 3
2019-10-05 05:14:13 +00:00
return r[0 : len(r) - len(p)] + p
def decodeBase64(text):
base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
s = ""
2019-10-05 05:14:13 +00:00
for i in text:
if i in base64chars:
s += i
c = ""
else:
2019-10-05 05:14:13 +00:00
if i == "=":
c += "="
p = ""
if c == "=":
2019-10-05 05:14:13 +00:00
p = "A"
else:
if c == "==":
p = "AA"
2019-10-05 05:14:13 +00:00
r = ""
s = s + p
2019-10-05 05:14:13 +00:00
i = 0
while i < len(s):
2019-10-05 05:14:13 +00:00
n = (
(base64chars.index(s[i]) << 18)
+ (base64chars.index(s[i + 1]) << 12)
+ (base64chars.index(s[i + 2]) << 6)
+ base64chars.index(s[i + 3])
)
r += chr((n >> 16) & 255) + chr((n >> 8) & 255) + chr(n & 255)
2019-10-05 05:14:13 +00:00
i += 4
2019-10-05 05:14:13 +00:00
return r[0 : len(r) - len(p)]
2018-10-28 21:36:51 +00:00
def main():
print(encodeBase64("WELCOME to base64 encoding"))
print(decodeBase64(encodeBase64("WELCOME to base64 encoding")))
2018-10-28 21:36:51 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
2018-10-28 21:36:51 +00:00
main()