Skip to content

Instantly share code, notes, and snippets.

@unix-beard
Last active August 29, 2015 14:13
Show Gist options
  • Save unix-beard/bd57485bdfd08c17ec5e to your computer and use it in GitHub Desktop.
Save unix-beard/bd57485bdfd08c17ec5e to your computer and use it in GitHub Desktop.
Convert hex to base64
#!/usr/bin/env python3
########################################################
# the matasano crypto challenges
# http://cryptopals.com/sets/1/challenges/1/
########################################################
from base64 import b64encode
from itertools import zip_longest
s = b'49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d'
# Will produce 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t'
print(b64encode(bytes(''.join([chr(int(chr(el[0]) + chr(el[1]), base=16)) for el in zip_longest(s[::2], s[1::2])]), 'utf-8')).decode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment