def decrypt_parameters_and_check_admin(input_string): cipher = ctr_module(key, nonce) plaintext = cipher.ctr(input_string) if b";admin=true;" in plaintext: return True else: return False
def encrypt_parameters(input_string): # Quote out '=' and ';' from input string and convert to bytes input_string = bytes( input_string.replace(';', '%3B').replace('=', '%3D'), 'utf-8') parameters_string = prefix + input_string + suffix cipher = ctr_module(key, nonce) ciphertext = cipher.ctr(parameters_string) return ciphertext
def edit(ciphertext, offset, new_text): key = b'YELLOW SUBMARINE' nonce = struct.pack('Q', 0) cipher = ctr_module(key, nonce) # Initialize cipher # Because of the keystream nature of CTR we can encrypt a specific character at position 'offset' fairly easily encrypted_new_text = cipher.ctr(bytes([0]) * offset + new_text) # Then, slice the original ciphertext and inject the new text encrypted prefix = ciphertext[:offset] injection = encrypted_new_text[offset:] suffix = ciphertext[offset + len(encrypted_new_text):] chosen_ciphertext = prefix + injection + suffix return chosen_ciphertext
def main(): # Set 01 - Challenge 07 target = base64.b64decode(open('set04challenge25.txt', 'r').read()) key = b'YELLOW SUBMARINE' cipher = AES.new(key, AES.MODE_ECB) plaintext = cipher.decrypt(target) # Proceeding with current challenge nonce = struct.pack('Q', 0) cipher = ctr_module(b'YELLOW SUBMARINE', struct.pack('Q', 0)) ciphertext = cipher.ctr(plaintext) recovered_plaintext = break_rw_ctr(ciphertext) if recovered_plaintext != plaintext: print("*** FAILED ***") print("--- Success ---")
''' import base64 import struct from set03challenge18 import ctr_module from os import urandom as get_random_bytes from set01challenge02 import xor_strings target = open('set03challenge19.txt', 'r').readlines() target_list = [base64.b64decode(line) for line in target] # Parameters key = get_random_bytes(16) nonce = struct.pack('Q', 0) # Expanded to 64bit little endian cipher = ctr_module(key, nonce) encrypted_target = [cipher.ctr(item) for item in target_list] def single_character_decryption(encrypted_target, i): # For each of the 256 possible guess-values: for guess in range(256): # Generate a list of all the 256 values of guess # XOR'd against the value at index i of each ciphertext decrypted = [ encrypted_item[i] ^ guess for encrypted_item in encrypted_target ] # If all the XOR'd values are characters in the list provided, # Then return the value of guess if all([ chr(x)