SEETF'23: BabyRC4
This was one of the “easier” crypto challenge in SEETF, using the ARC4 (an implementation of RC4 cipher, a symmetric stream cipher).
The Challenge
Explanation
Looking at the code, the challenge involves requiring to decrypt the flag value by reversing the encryption process. It gives two ciphertexts: c0
and c1
. The objective was to decrypt the flag, represented as b'SEE{?????????????????????????????????}'[::-1]
.
In essence, the ARC4 XORs each byte of the plaintext with a corresponding byte from a pseudorandom key (generated from the key
variable). XOR is a binary operation which compares two binary values (plaintext and key) to produce an encrypted result. Read more here.
But lets look closer at the code.
- First, the code generates a random 16-byte (128-bit) key using the
urandom
function. - The
enc
function is defined to encrypt the plaintext with the ARC4 algorithm with the generated key, taking the plaintext as the input, initialises an ARC4 cipher with the key, then encrypts the plaintext. - Two ciphertexts, c0 (result of the flag being encrytped) and c1 (result of 36 ‘a’s being encrypted), are provided in hexadecimal format, which are the results of encrypting different plaintexts using the same key.
Solution
- Knowing that the two ciphertexts are encrypted with the same key, the first step was to XOR
c0
andc1
byte by byte to obtain the keystream.
- The next step was to recover the key. With the keystream in hand, and knowing that
c1
was XOR-ed using a sequence of ‘a’s, we could XOR the keystream with ‘a’s to cancel out the keystream. This gave us our key!
- Now that we have our key, I converted it to hexadecimal, printed it out to take a look at it, and then assigned a variable
recovered_key_hex
- With the recovered key in hexadecimal, we could now recover the key in ascii and reverse it to retrieve the key. I also had to append the
SE
as the prefix to the flag, assuming that it wasn’t added during encryption.
And the flag I got was SEE{n3vEr_reU53_rC4_k3y5ss5s:cafe2835}
.