def test_ch17(self): key = generate_random_key() oracle = encryption_get_oracle_func(key, cbc_padding_oracle) lst = [ "MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=", "MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic=", "MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw==", "MDAwMDAzQ29va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg==", "MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl", "MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA==", "MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw==", "MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8=", "MDAwMDA4b2xsaW4nIGluIG15IGZpdmUgcG9pbnQgb2g=", "MDAwMDA5aXRoIG15IHJhZy10b3AgZG93biBzbyBteSBoYWlyIGNhbiBibG93" ] for plaintext in lst: #test funcs ciphertext = produce_ciphertext(plaintext, key) self.assertTrue(oracle(ciphertext)) self.assertEqual( unpadpkcs7(break_cbc_padding_oracle(oracle, ciphertext)), plaintext)
def test_ch25(self): key = generate_random_key(16) with open('static/25.txt', 'r') as myfile: cipher = b64decode(myfile.read()) plaintext = unpadpkcs7(aes_ecb_decrypt(cipher, "YELLOW SUBMARINE")) nonce = form_nonce(0) ciphertext = aes_ctr_encrypt_decrypt(plaintext, key, nonce) edit_func = partial(edit_ciphertext, key=key) # test edit_ciphertext function result = edit_ciphertext(ciphertext, key, 32, "YELLOW") self.assertEquals( result, aes_ctr_encrypt_decrypt(plaintext[:32] + "YELLOW" + plaintext[38:], key, nonce)) # break it self.assertEquals(break_edit_ciphertext(ciphertext, edit_func), plaintext)
def decryption_oracle(ciphertext, key, blocksize=16): iv = ciphertext[:blocksize] return unpadpkcs7(aes_cbc_decrypt(ciphertext[blocksize:], key, iv))
def decryption_oracle(ciphertext, key, blocksize=16): iv = key blocks = aes_cbc_decrypt(ciphertext, key, iv) if not all(map(lambda x: x in range(0, 128), blocks)): raise Not7BitAscii(blocks) return unpadpkcs7(blocks)
def decryption_oracle(cipher, key): return key_value_parse(unpadpkcs7(aes_ecb_decrypt(cipher, key)))
def test_ch10(self): with open("static/10.txt", "r") as myfile: cipher = b64decode(myfile.read()) key = "YELLOW SUBMARINE" self.assertEqual(unpadpkcs7(aes_cbc_decrypt(cipher, key)), self.poem)