def CBC_encrypt(data, key, IV): l = len(data) b = randint(5, 10) data = random_key_generator(b) + data + random_key_generator(randint( 5, 10)) data = pad(data, 16) encryptor = AES.new(key, AES.MODE_CBC, IV=IV) return encryptor.encrypt(data)[b:b + l]
def ecb_oracle(plain_bytes, block_size): if isinstance(plain_bytes, str): plain_bytes = plain_bytes.encode('ascii') #secret_string = b64decode(fake_string) secret_string = b64decode(to_decrypt) plain_bytes += secret_string plain_bytes = pad(plain_bytes, block_size) return encrypt_AES_ECB(plain_bytes, en_key)
def ECB_encrypt(data, key): l = len(data) b = randint(5, 10) data = random_key_generator(b) + data + random_key_generator(randint( 5, 10)) data = pad(data, 16) mode = AES.new(key, AES.MODE_ECB) return mode.encrypt(data)[b:l + b]
def encrypt_AES_CBC(plaintext, key): plaintext = plaintext.replace('&', '').replace('=', '') plaintext = "comment1=cooking%20MCs;userdata=" + plaintext + ";comment2=%20like%20a%20pound%20of%20bacon" plaintext = plaintext.encode('ascii') print(keysize_blocks(BLOCK_SIZE, plaintext)) padded_plaintext = pad(plaintext, BLOCK_SIZE) cipher_obj = AES.new(key, AES.MODE_CBC, iv) encrypted_bytes = cipher_obj.encrypt(padded_plaintext) return encrypted_bytes
def encryption_oracle(plain_bytes, block_size): key = urandom(block_size) append = urandom(randint(5, 10)) preppend = urandom(randint(5, 10)) #plain_bytes = append + plain_bytes + preppend plain_bytes = pad(plain_bytes, block_size) print("Plainbyte length: ", len(plain_bytes)) if randint(0, 1): encrypted = encrypt_AES_ECB(plain_bytes, key) #print("Making ECB") return encrypted else: iv = urandom(block_size) encrypted = encrypt_AES_CBC(plain_bytes, iv, key, block_size) #print("Making CBC") return encrypted
def encrypt_profile(encoded_str, key): encoded_str = encoded_str.encode('ascii') encoded_str = pad(encoded.encode('ascii'), BLOCK_SIZE) return encrypt_AES_ECB(encoded_str, key)
key = urandom(BLOCK_SIZE) iv = urandom(BLOCK_SIZE) sample = """MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc= MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic= MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw== MDAwMDAzQ29va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg== MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA== MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw== MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8= MDAwMDA4b2xsaW4nIGluIG15IGZpdmUgcG9pbnQgb2g= MDAwMDA5aXRoIG15IHJhZy10b3AgZG93biBzbyBteSBoYWlyIGNhbiBibG93""" clean_sample = sample.split("\n") clean_sample = [string.strip(" ") for string in clean_sample] plaintext = pad(b64decode(choice(clean_sample)), BLOCK_SIZE) #plaintext = pad(clean_sample[1].encode('ascii'), BLOCK_SIZE) print(plaintext) cipher_text_original = encrypt_func(plaintext, key, iv) #cipher_text = bytearray(16) + cipher_text_original cipher_text = bytearray(b'0' * BLOCK_SIZE) + cipher_text_original #import pdb; pdb.set_trace() print("Cipher text is: ", cipher_text) decrypted = decrypt_func(cipher_text, key, iv) # To derive intermediate # P = Plaintext, I = intermediate, E = 16 # P16 = I16 ^ E16 # x01 = I16 ^ E'16
def detection_oracle(data): c = 0 if detect_ECB(pad(data)): c = 1 return c
iv = intermediate while ciphertext: intermediate = ciphertext.pop(0) decrypted_bytes = decrypt_AES_ECB_bytes(intermediate, key) plaintext += xor_bytes(decrypted_bytes, iv) iv = intermediate return plaintext if __name__ == '__main__': BLOCK_SIZE = 16 iv = ("\x00" * BLOCK_SIZE).encode('ascii') key = pad("secret_password".encode('ascii'), BLOCK_SIZE) message = pad("hello world, the quick brown fox".encode('ascii'), BLOCK_SIZE) x = encrypt_AES_CBC(message, iv, key, BLOCK_SIZE) #print(x) #x = encrypt_AES_ECB(message, key) #print(x) #y = decrypt_AES_ECB_bytes(x, key) #print(y) y = decrypt_AES_CBC(x, iv, key, BLOCK_SIZE) print(y.decode('ascii')) key2 = pad("YELLOW SUBMARINE".encode('ascii'), BLOCK_SIZE) cipher_message = base64_file_to_bytes('10.txt') decrypted = decrypt_AES_CBC(cipher_message, iv, key2, BLOCK_SIZE) print(decrypted.decode('ascii'))