def main(): with open("6.txt", "r") as f: ciphertext_b64 = f.read().replace("\n", "") ciphertext = base64_to_bytearray(ciphertext_b64) key = break_repeating_key_xor(ciphertext) print("challenge 1.6 key is: %s" % bytearray(key)) message = encrypt_repeating_key(ciphertext, key)
def main(): test_aes_ecb() with open("7.txt", "r") as f: ciphertext_b64 = f.read().replace("\n", "") ciphertext_bt = base64_to_bytearray(ciphertext_b64) key = string_to_bytearray("YELLOW SUBMARINE") message = decrypt_aes_128_ecb(ciphertext_bt, key) assert b"I'm back and I'm ringin' the bell" in message print("challenge 1.7 completed.")
def cbc_encryption_oracle(): messages_encrypted = [] for message in messages: message_to_encrypt = base64_to_bytearray(message) message_to_encrypt_padded = pad_to_mod_16(message_to_encrypt) ciphertext = encrypt_aes_cbc(message_to_encrypt_padded, ORACLE_KEY, ORACLE_IV) messages_encrypted.append(ciphertext) return messages_encrypted
def main(): plaintext_b64 = "VGhhdCdzIHdoeSBJIGZvdW5kIHlvdSBkb24ndCBwbGF5IGFyb3VuZCB3aXRoIHRoZSBGdW5reSBDb2xkIE1lZGluYQ==" plaintext = str(base64_to_bytearray(plaintext_b64)) ciphertext = ORACLE_KEY.encrypt(plaintext) found_plaintext = attack_parity_oracle(ciphertext, ORACLE_KEY.e, ORACLE_KEY.N) if found_plaintext == plaintext: print("challenge 5.46 completed.") else: print("challenge 5.46 failed.")
def main(): test_aes_ctr() ciphertext_b64 = 'L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==' ciphertext_bt = base64_to_bytearray(ciphertext_b64) key = string_to_bytearray("YELLOW SUBMARINE") nonce = bytearray([0] * 8) message = decrypt_aes_ctr(ciphertext_bt, key, nonce) #print message assert b"VIP Let's kick it" in message print("challenge 3.18 completed.")
def main(): test_aes_cbc() with open("10.txt", "r") as f: ciphertext_b64 = f.read().replace("\n", "") ciphertext_bt = base64_to_bytearray(ciphertext_b64) key = string_to_bytearray("YELLOW SUBMARINE") iv = bytearray([0] * 16) message = decrypt_aes_cbc(ciphertext_bt, key, iv) assert b"I'm back and I'm ringin' the bell" in message print("challenge 2.10 completed.")
from aes_ecb_mode import encrypt_aes_128_ecb from pkcs7_padding import pad_to_mod_16 from ecb_cbc_detection_oracle import detection_oracle import byte_at_a_time_ecb_decryption_simple as byte_at_a_time_simple from utils import chunks_of_bytearray from itertools import groupby oracle_key = crypto_random_bytes(16) secret_b64_string = ''' Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg YnkK ''' secret = base64_to_bytearray(secret_b64_string) padding = crypto_random_bytes(random_integer(1, 30)) def encryption_oracle_ecb(plaintext): to_encrypt = pad_to_mod_16(padding + plaintext + secret) return encrypt_aes_128_ecb(to_encrypt, oracle_key) def _check_magic_blocks_number(magic, ct, block_size): blocks = chunks_of_bytearray(ct, block_size) repeating_blocks_grouped = [list(j) for i, j in groupby(blocks)] repeating_blocks_sizes = [len(i) for i in repeating_blocks_grouped] # padding is multiple of block_size if magic in repeating_blocks_sizes:
for xor_cipher in xor_ciphers: score, message, key = break_xor_cipher(xor_cipher, alpha_and_printable_heuristic) key_used.append(key) return key_used BLOCK_SIZE = 16 KEY = crypto_random_bytes(BLOCK_SIZE) NONCE = bytearray([0] * 8) with open('20.txt', 'r') as f: lines = f.readlines() messages_b64 = [line.replace("\n", "") for line in lines] messages = [base64_to_bytearray(i) for i in messages_b64] ciphertexts = [encrypt_aes_ctr(i, KEY, NONCE) for i in messages] key_stream_length = max([len(i) for i in ciphertexts]) key_stream = [0] * key_stream_length def main(): key_stream = break_ctr(ciphertexts) messages_decrypted = [xor_diff_lengths(key_stream, i) for i in ciphertexts] # for ix, m in enumerate(messages_decrypted): # print(ix, m) message_corrected = b"You think you're ruffer, then suffer the consequences"