Skip to content

Instantly share code, notes, and snippets.

@WitherOrNot
Created June 14, 2019 04:55
Show Gist options
  • Save WitherOrNot/cc29a15678737b09203f92846f8885d8 to your computer and use it in GitHub Desktop.
Save WitherOrNot/cc29a15678737b09203f92846f8885d8 to your computer and use it in GitHub Desktop.
Code golf for base64 encoder/decoder
#"""
#Encoder
c=[chr(x) for x in range(65,91)+range(97,123)+range(48,58)+[43,47]];a=raw_input();p=(3-(len(a)%3))%3;a+="\x00"*p;a=''.join(["0"*(8-len(x))+x for x in [bin(ord(x)).replace("0b","") for x in a]]);a=[c[int(x,2)] for x in [a[i:i+6] for i in range(0,len(a),6)]];a=''.join(a[:(len(a)-p)])+"="*p;print(a)
#"""
"""
#Decoder
c=[chr(x) for x in range(65,91)+range(97,123)+range(48,58)+[43,47,61]];a=raw_input();a=''.join(["0"*(6-len(x))+x for x in [bin(c.index(x)%64).replace("0b","") for x in a]]);a=''.join([chr(int(a[i:i+8],2)) for i in range(0,len(a),8)]);print(a)
#"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment