Skip to content

Instantly share code, notes, and snippets.

@crashish
Last active September 19, 2022 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crashish/30999a5b53fc74eedc21 to your computer and use it in GitHub Desktop.
Save crashish/30999a5b53fc74eedc21 to your computer and use it in GitHub Desktop.
A simple implementation of a base64 decoder that supports alternative alphabets
def b64decode(string,alphabet):
string = string.replace("=","")
ret = ""
left = 0
for i in range(0, len(string)):
if left == 0:
left = 6
else:
value1 = alphabet.index(string[i - 1]) & (2 ** left - 1)
value2 = alphabet.index(string[i]) >> (left - 2)
value = (value1 << (8 - left)) | value2
ret += chr(value)
left -= 2
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment