diff --git a/src/padding.py b/src/padding.py index b4baa6a..abfcb0e 100644 --- a/src/padding.py +++ b/src/padding.py @@ -20,3 +20,14 @@ def strip_pkcs7_pad(block, block_size=16): if last_byte > 0 and last_byte < block_size: return block[0:(-1*last_byte)] return block + +def throw_bad_padding(block, block_size=16): + block_len = len(block) + if block_len % block_size == 0: + # check last byte + last_byte = block[-1] + pad_bytes = block[(-1*last_byte):] + for pad_byte in pad_bytes: + if pad_byte != last_byte: + raise Exception("Bad padding") + diff --git a/tests/test_set2.py b/tests/test_set2.py index cb0eedf..807b472 100644 --- a/tests/test_set2.py +++ b/tests/test_set2.py @@ -148,9 +148,18 @@ def test_set2_challenge14(self): plaintext = b''.join([i.to_bytes() for i in plaintext_bytes]) self.assertEqual(expected, plaintext) # cool af - return def test_set2_challenge15(self): - return + cases = [ + b'ICE ICE BABY\x04\x04\x04\x04', + b'ICE ICE BABY\x05\x05\x05\x05', + b'ICE ICE BABY\x01\x02\x03\x04' + ] + for c in cases: + try: + padding.throw_bad_padding(c) + except Exception as e: + self.assertRaises(Exception, e) + def test_set2_challenge16(self): return