2 minutes
Cryptopals Set 1 - Challenge 1
Welcome to another Forest Hex crypto adventure! 🌲🏹
Today I will be walking through the first Cryptopals challenge.
Cryptopals is a series of cryptology related challenges broken into sets, escalating in difficulty. I was recommended to check out this site by another user in a Nintendo Switch Hacking discord server.You can find this server here: https://discord.gg/ZdqEhed
In fact, a lot of my recent drive to learn hacking was watching the Nintendo Switch hacking scene unfold. It was extremely interesting to see the breadth, and depth of knowledge that came together.
Anyway, on to the first challenge. Here is the description on the site:
https://cryptopals.com/sets/1/challenges/1
Convert hex to base64
The string: 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
Should produce: SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
So go ahead and make that happen. You'll need to use this code for the rest of the exercises.
Cryptopals Rule
Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing.
This is a simple conversion of hex to base64. This is easily achievable with Python.
Something that may be easily missed: Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing.
In order to achieve this:
- Input = Hex String
- Convert input to bytes.
- Convert bytes to base 64.
- Print result.
Python includes a handy way to convert a hex string to bytes: bytes.fromhex()
It also has a standard library base64
which includes base64.b64encode()
So we simply call these functions in the right order and print the result.
Here is the code that achieves this:
import base64
def hex_to_b64(input):
return base64.b64encode(bytes.fromhex(input))
print(hex_to_b64('49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d'))
Our function properly prints
b'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t'
The b
in front of the string means it’s a bytes type instead of a string.