This starter course gets you up and running with CryptoHack. You'll learn to encode and decode data types that are commonly used in cryptography. Then you'll get comfortable with the XOR operation which is at the centre of symmetric cryptography. Finally, the course ends with some fun XOR puzzles to test what you've learned.
In this writeup I shall cover my solutions to the problems that are available in this introductory course module.
Each challenge is designed to help introduce you to a new piece of cryptography. Solving a challenge will require you to find a "flag".
These flags will usually be in the format crypto{y0ur_f1rst_fl4g}
. The flag format helps you verify that you found the correct solution.
Try submitting this flag into the form below to solve your first challenge.
crypto{y0ur_f1rst_fl4g}
Modern cryptography involves code, and code involves coding. CryptoHack provides a good opportunity to sharpen your skills.
Of all modern programming languages, Python 3 stands out as ideal for quickly writing cryptographic scripts and attacks.
Run the attached Python script and it will output your flag.
Challenge files:
Resources:
We are basically provided with a python script, that we are supposed to run to get the flag, reviewing the script we can see that it's basically a script to perform XOR (Exclusive OR Operation):
Thus running this the operation shall result to the plaintext flag:
crypto{z3n_0f_pyth0n}
ASCII is a 7-bit encoding standard which allows the representation of text using the integers 0-127.
Using the below integer array, convert the numbers to their corresponding ASCII characters to obtain a flag.
ββββ In Python, the chr() function can be used to convert an ASCII ordinal number to a character (the ord() function does the opposite).
By writing a python script, I am able to solve this as seen below:
crypto{ASCII_pr1nt4bl3}
When we encrypt something the resulting ciphertext commonly has bytes which are not printable ASCII characters. If we want to share our encrypted data, it's common to encode it into something more user-friendly and portable across different systems.
Hexadecimal can be used in such a way to represent ASCII strings. First each letter is converted to an ordinal number according to the ASCII table (as in the previous challenge). Then the decimal numbers are converted to base-16 numbers, otherwise known as hexadecimal. The numbers can be combined together, into one long hex string.
Included below is a flag encoded as a hex string. Decode this back into bytes to get the flag.
ββββ In Python, the bytes.fromhex() function can be used to convert hex to bytes. The .hex() instance method can be called on byte strings to get the hex representation.
Resources:
Using the utility xxd we can easily decode the hex to plaintext via shell/terminal with the command:
By writing the following python script we can easily decode the hex and get the plaintext readable string:
Another common encoding scheme is Base64, which allows us to represent binary data as an ASCII string using an alphabet of 64 characters. One character of a Base64 string encodes 6 binary digits (bits), and so 4 characters of Base64 encode three 8-bit bytes.
Base64 is most commonly used online, so binary data such as images can be easily included into HTML or CSS files.
Take the below hex string, decode it into bytes and then encode it into Base64.
ββββ In Python, after importing the base64 module with import base64, you can use the base64.b64encode() function. Remember to decode the hex first as the challenge description states.
Using the utility base64 we can easily decode the hex then decode the base64 to get the readable output using the following command:
Writing the following python script we can easily achieve to get the required flag:
Cryptosystems like RSA works on numbers, but messages are made up of characters. How should we convert our messages into numbers so that mathematical operations can be applied?
The most common way is to take the ordinal bytes of the message, convert them into hexadecimal, and concatenate. This can be interpreted as a base-16/hexadecimal number, and also represented in base-10/decimal.
To illustrate:
ββββ Python's PyCryptodome library implements this with the methods bytes_to_long() and long_to_bytes(). You will first have to install PyCryptodome and import it with from Crypto.Util.number import *. For more details check the FAQ.
Convert the following integer back into a message:
First we need to install pycryptodome using the command pip3 install pycryptodome
then we write the following python script to convert the integer back to a plaintext message:
which eventually should provide us with the plaintext message after running it:
crypto{3nc0d1n6_4ll_7h3_w4y_d0wn}
XOR is a bitwise operator which returns 0 if the bits are the same, and 1 otherwise. In textbooks the XOR operator is denoted by β, but in most challenges and programming languages you will see the caret ^
used instead.
A | B | Output |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
For longer binary numbers we XOR bit by bit: 0110 ^ 1010 = 1100. We can XOR integers by first converting the integer from decimal to binary. We can XOR strings by first converting each character to the integer representing the Unicode character.
For longer binary numbers we XOR bit by bit: 0110 ^ 1010 = 1100. We can XOR integers by first converting the integer from decimal to binary. We can XOR strings by first converting each character to the integer representing the Unicode character.
Given the string label, XOR each character with the integer 13. Convert these integers back to a string and submit the flag as crypto{new_string}.
ββββ The Python pwntools library has a convenient xor() function that can XOR together data of different types and lengths. But first, you may want to implement your own function to solve this.
We write a python script that will perform XOR operation on each byte of the word label
and provide the output:
Running this we should be able to get the required output:
crypto{aloha}
In the last challenge, you saw how XOR worked at the level of bits. In this one, we're going to cover the properties of the XOR operation and then use them to undo a chain of operations that have encrypted a flag. Gaining an intuition for how this works will help greatly when you come to attacking real cryptosystems later, especially in the block ciphers category.
There are four main properties we should consider when we solve challenges using the XOR operator
Let's break this down. Commutative means that the order of the XOR operations is not important. Associative means that a chain of operations can be carried out without order (we do not need to worry about brackets). The identity is 0, so XOR with 0 "does nothing", and lastly something XOR'd with itself returns zero.
Let's put this into practice! Below is a series of outputs where three random keys have been XOR'd together and with the flag. Use the above properties to undo the encryption in the final line to obtain the flag.
ββββ Before you XOR these objects, be sure to decode from hex to bytes.
To simply solve this I first install pwntools using the command pip3 install pwntools
then I write the below script:
Running it should provide the flag:
crypto{x0r_i5_ass0c1at1v3}
For the next few challenges, you'll use what you've just learned to solve some more XOR puzzles.
I've hidden some data using XOR with a single byte, but that byte is a secret. Don't forget to decode from hex first.
Using the following script with pwntools
I am able to get the flag required,
The flag is:
crypto{0x10_15_my_f4v0ur173_by7e}
I've encrypted the flag with my secret key, you'll never be able to guess it.
ββββRemember the flag format and how it might help you in this challenge!
Using cyberchef, I was able to get the plaintext flag;
When using the key crypto{
we get the output which contains the string myXORkey
using this as the key we get the flag:
crypto{1f_y0u_Kn0w_En0uGH_y0u_Kn0w_1t_4ll}
And that's it! That's the whole Introduction to CryptoHack course problems that we just solved!