def test_remove_pkcs7_padding(): """ Set 2, Challenge 15 """ assert remove_pkcs7_padding('ICE ICE BABY\x04\x04\x04\x04') == 'ICE ICE BABY' with pytest.raises(Exception): remove_pkcs7_padding('ICE ICE BABY\x05\x05\x05\x05') with pytest.raises(Exception): remove_pkcs7_padding('ICE ICE BABY\x01\x02\x03\x04')
def cbc_padding_oracle(ciphertext, iv, padding_valid_fn): """ Decrypt a CBC encrypted ciphertext using a padding oracle :param ciphertext: Ciphertext to decrypt :param padding_valid_fn: Function that decrypts the ciphertext, checks the padding, and returns the status of the padding (True or False) :return: Recovered plaintext """ block_size = 16 ct_blocks = list(chunks(iv + ciphertext, block_size)) pt = '' for two_blocks in chunks(ct_blocks[::-1], 2, adv=1): try: pt = cbc_padding_attack_block(two_blocks[1], two_blocks[0], padding_valid_fn) + pt except IndexError: break return remove_pkcs7_padding(pt)