def edit_fast(cipher, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed); text = Crypto.decrypt_aes( cipher, key, AES.MODE_CTR, counter=counter) text = newtext counter = Crypto.gen_aes_stream_counter_mt19973(seed); return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter)
def edit(cipher, offset, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed); text = Crypto.decrypt_aes( cipher, key, AES.MODE_CTR, counter=counter) text = text[:offset] + newtext + text[offset + 1:] counter = Crypto.gen_aes_stream_counter_mt19973(seed); return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter)
def test_break_random_access_read_write(self): """Challenge 25""" f = open('data/25.txt') data = f.read() f.close() key = 'YELLOW SUBMARINE' known = Crypto.decrypt_aes(base64.b64decode(data), key, AES.MODE_ECB) seed = 23232232; def edit(cipher, offset, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed); text = Crypto.decrypt_aes( cipher, key, AES.MODE_CTR, counter=counter) text = text[:offset] + newtext + text[offset + 1:] counter = Crypto.gen_aes_stream_counter_mt19973(seed); return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter) def edit_fast(cipher, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed); text = Crypto.decrypt_aes( cipher, key, AES.MODE_CTR, counter=counter) text = newtext counter = Crypto.gen_aes_stream_counter_mt19973(seed); return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter) counter = Crypto.gen_aes_stream_counter_mt19973(seed); cipher = Crypto.encrypt_aes(known, key, AES.MODE_CTR, counter=counter) text = edit_fast(cipher, cipher) # replacing text with cipher is same is decrypting cipher. #for i in range(len(cipher)): # text += edit(cipher, i, cipher[i])[i] self.assertEqual(known, text)
def test_break_if_iv_is_same_as_key(self): """Challenge 27""" key = Crypto.gen_random_key(16) iv = key oracle = lambda c: Crypto.decrypt_aes(c, key, AES.MODE_CBC, iv) cipher = Crypto.encrypt_aes("X" * 48, key, AES.MODE_CBC, iv) self.assertEquals( key, Crypto.extract_key_if_key_is_same_as_key(cipher, oracle))
def test_break_if_iv_is_same_as_key(self): """Challenge 27""" key = Crypto.gen_random_key(16) iv = key oracle = lambda c: Crypto.decrypt_aes(c, key, AES.MODE_CBC, iv) cipher = Crypto.encrypt_aes("X"*48, key, AES.MODE_CBC, iv) self.assertEquals(key, Crypto.extract_key_if_key_is_same_as_key(cipher, oracle))
def edit_fast(cipher, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed) text = Crypto.decrypt_aes(cipher, key, AES.MODE_CTR, counter=counter) text = newtext counter = Crypto.gen_aes_stream_counter_mt19973(seed) return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter)
def edit(cipher, offset, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed) text = Crypto.decrypt_aes(cipher, key, AES.MODE_CTR, counter=counter) text = text[:offset] + newtext + text[offset + 1:] counter = Crypto.gen_aes_stream_counter_mt19973(seed) return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter)
def test_aes_ctr_encryption(self): """Challenge 18""" counter = Crypto.gen_aes_stream_counter_simple() key = "YELLOW SUBMARINE" cipher = base64.b64decode("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLX" +\ "zhPweyyMTJULu/6/kXX0KSvoOLSFQ==") text = "Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby " self.assertEqual(text, Crypto.decrypt_aes( cipher, key, AES.MODE_CTR, counter=counter)) # another test key = Crypto.gen_random_key(16) text = "Let there be light!" counter = Crypto.gen_aes_stream_counter_simple() cipher = Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter) counter = Crypto.gen_aes_stream_counter_simple() self.assertEqual(text, Crypto.decrypt_aes( cipher, key, AES.MODE_CTR, counter=counter))
def test_break_aes_padding_leak(self): """Challenge 17""" quote = lambda text: text _, key, init_vector = Crypto.generate_aes_oracle( '', '', AES.MODE_CBC, quote) def has_valid_padding(cipher, init_vector): """Checks if cipher has valid padding.""" try: Crypto.decrypt_aes(cipher, key, AES.MODE_CBC, init_vector) except ValueError: return False return True lines = [base64.b64decode(l) for l in open('data/17.txt').readlines()] for line in lines: cipher = Crypto.encrypt_aes(line, key, AES.MODE_CBC, init_vector) self.assertEqual(line, Crypto.break_aes_using_padding_leak( cipher, init_vector, has_valid_padding))
def test_break_aes_padding_leak(self): """Challenge 17""" quote = lambda text: text _, key, init_vector = Crypto.generate_aes_oracle( '', '', AES.MODE_CBC, quote) def has_valid_padding(cipher, init_vector): """Checks if cipher has valid padding.""" try: Crypto.decrypt_aes(cipher, key, AES.MODE_CBC, init_vector) except ValueError: return False return True lines = [base64.b64decode(l) for l in open('data/17.txt').readlines()] for line in lines: cipher = Crypto.encrypt_aes(line, key, AES.MODE_CBC, init_vector) self.assertEqual( line, Crypto.break_aes_using_padding_leak(cipher, init_vector, has_valid_padding))
def test_aes_ctr_encryption(self): """Challenge 18""" counter = Crypto.gen_aes_stream_counter_simple() key = "YELLOW SUBMARINE" cipher = base64.b64decode("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLX" +\ "zhPweyyMTJULu/6/kXX0KSvoOLSFQ==") text = "Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby " self.assertEqual( text, Crypto.decrypt_aes(cipher, key, AES.MODE_CTR, counter=counter)) # another test key = Crypto.gen_random_key(16) text = "Let there be light!" counter = Crypto.gen_aes_stream_counter_simple() cipher = Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter) counter = Crypto.gen_aes_stream_counter_simple() self.assertEqual( text, Crypto.decrypt_aes(cipher, key, AES.MODE_CTR, counter=counter))
def test_break_random_access_read_write(self): """Challenge 25""" f = open('data/25.txt') data = f.read() f.close() key = 'YELLOW SUBMARINE' known = Crypto.decrypt_aes(base64.b64decode(data), key, AES.MODE_ECB) seed = 23232232 def edit(cipher, offset, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed) text = Crypto.decrypt_aes(cipher, key, AES.MODE_CTR, counter=counter) text = text[:offset] + newtext + text[offset + 1:] counter = Crypto.gen_aes_stream_counter_mt19973(seed) return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter) def edit_fast(cipher, newtext): counter = Crypto.gen_aes_stream_counter_mt19973(seed) text = Crypto.decrypt_aes(cipher, key, AES.MODE_CTR, counter=counter) text = newtext counter = Crypto.gen_aes_stream_counter_mt19973(seed) return Crypto.encrypt_aes(text, key, AES.MODE_CTR, counter=counter) counter = Crypto.gen_aes_stream_counter_mt19973(seed) cipher = Crypto.encrypt_aes(known, key, AES.MODE_CTR, counter=counter) text = edit_fast(cipher, cipher) # replacing text with cipher is same is decrypting cipher. #for i in range(len(cipher)): # text += edit(cipher, i, cipher[i])[i] self.assertEqual(known, text)