Tags: crypto
Rating:
Author: shlswnt
Description: I have encrypted some text but it seems I have lost the key! Can you find it? <br>
Unlock Hint for 0 points: A string of text can be decrypted by merely reapplying the XOR function with the key
Given th first chall crypto beginner, this is a classic XOR encryption problem. The plaintext is XORed with a key to get the ciphertext. If we lost the key but have the plaintext and the ciphertext, we can retrieve the key by XORing the plaintext and the ciphertext.
import base64
plaintext = "A string of text can be encrypted by applying the bitwise XOR operator to every character using a given key"
key = "" # I have lost the key!
def byte_xor(ba1, ba2):
return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])
ciphertext_b64 = base64.b64encode(byte_xor(key.encode(), plaintext.encode()))
ciphertext_decoded = base64.b64decode("NkMHEgkxXjV/BlN/ElUKMVZQEzFtGzpsVTgGDw==")
print(ciphertext_decoded)
Here's how we can modify the code to find the key:
import base64
plaintext = "A string of text can be encrypted by applying the bitwise XOR operator to every character using a given key"
def byte_xor(ba1, ba2):
return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])
ciphertext_decoded = base64.b64decode("NkMHEgkxXjV/BlN/ElUKMVZQEzFtGzpsVTgGDw==")
key = byte_xor(plaintext.encode(), ciphertext_decoded)
print(key)
This will print the key that was used to encrypt the plaintext.
Flag:
wctf{X0R_i5_f0rEv3r_My_L0Ve}