def main(): """NOTE: After writing this code I realized that the approach I was using was the S3C20 solution. To solve this problem with a manual approach there are other ways. For example, we could start by considering, for each position, the key (byte) which creates the most spaces (since the spaces are very frequent), and then find patterns manually by guesses. I like to use a Jupyter notebook for manually playing with these kind of challenges where automating the steps is difficult and there has to be human intervention. Maybe I will add a manual solution to this challenge at some point. """ original_plaintexts = [] ciphertexts = [] random_key = Random.new().read(AES.key_size[0]) with open("S3C19_input.txt") as f: for line in f: original_plaintext = b64decode(line) original_plaintexts.append(original_plaintext) ciphertexts.append(aes_ctr(original_plaintext, random_key, 0)) cracked_plaintexts = crack_ctr_same_nonce(ciphertexts) # Print each cracked plaintext. Some of them will be slightly different from the original plaintext # but the attack is not perfect and as long as they are similar I would say that it worked. for plaintext, original in zip(cracked_plaintexts, original_plaintexts): print(plaintext)
def main(): """I actually used the suggested "statistic" approach to solve S3C19, so I will reuse the same code here.""" original_plaintexts = [] ciphertexts = [] random_key = Random.new().read(AES.key_size[0]) with open("S3C20_input.txt") as f: for line in f: original_plaintext = b64decode(line) original_plaintexts.append(original_plaintext) ciphertexts.append(aes_ctr(original_plaintext, random_key, 0)) cracked_plaintexts = crack_ctr_same_nonce(ciphertexts) # Print each cracked plaintext. Some of them will be slightly different from the original plaintext # but the attack is not perfect and as long as they are similar I would say that it worked. for plaintext, original in zip(cracked_plaintexts, original_plaintexts): print(plaintext)
def decrypt_and_check_admin(self, ciphertext): """Decrypts the string and returns whether the characters ";admin=true;" are in the string""" data = aes_ctr(ciphertext, self._key, self._nonce) return b';admin=true;' in data
def encrypt(self, data): """Adds the prefix and the suffix specified in the challenge and encrypts the data with AES-CTR""" data = data.decode().replace(';', '').replace( '=', '') # Remove special characters to avoid injection plaintext = (self._prefix + data + self._suffix).encode() return aes_ctr(plaintext, self._key, self._nonce)
def encrypt(self, plaintext): """Encrypts the given plaintext with AES-CTR with a nonce of 0.""" return aes_ctr(plaintext, self._key, 0)