def aes_ctr_encryption_oracle(plaintext, key, blocksize=16): suffix = ";comment2=%20like%20a%20pound%20of%20bacon" prefix = "comment1=cooking%20MCs;userdata=" plaintext = clean(plaintext) iv = Random.new().read(blocksize) nonce = form_nonce(0) return aes_ctr_encrypt_decrypt(prefix + plaintext + suffix, key, nonce)
def test_ch18(self): cipher = b64decode( "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==" ) key = "YELLOW SUBMARINE" nonce = form_nonce(0) self.assertEqual( aes_ctr_encrypt_decrypt(cipher, key, nonce), "Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby ")
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 test_ch20(self): plaintexts = [] ciphertexts = [] key = generate_random_key(16) with open('static/19.txt', 'r') as myfile: for line in myfile.read().splitlines(): plaintexts.append(b64decode(line)) for plaintext in plaintexts: nonce = form_nonce(0) ciphertexts.append(aes_ctr_encrypt_decrypt(plaintext, key, nonce)) raise NotImplementedError
def test_ch19(self): # this is a many time pad problem plaintexts = [] ciphertexts = [] key = generate_random_key(16) with open('static/19.txt', 'r') as myfile: for line in myfile.read().splitlines(): plaintexts.append(b64decode(line)) for plaintext in plaintexts: nonce = form_nonce(0) ciphertexts.append(aes_ctr_encrypt_decrypt(plaintext, key, nonce)) break_ctr_reused_nonce_substitutions(ciphertexts) raise NotImplementedError
def edit_ciphertext(ciphertext, key, offset, newtext): nonce = form_nonce(0) f = aes_ctr_encrypt_decrypt( ciphertext[:offset] + newtext + ciphertext[offset + len(newtext):], key, nonce)[offset:offset + len(newtext)] return ciphertext[:offset] + f + ciphertext[offset + len(newtext):]