def test_pkcs7_pad_invalid_block_size(): with pytest.raises(ValueError): set2.pkcs7_pad(b"YELLOW SUBMARINE", 0) with pytest.raises(ValueError): set2.pkcs7_pad(b"YELLOW SUBMARINE", -5) with pytest.raises(ValueError): set2.pkcs7_pad(b"YELLOW SUBMARINE", 14.3)
def encrypt_user_data(plaintext, key, iv): return encrypt_aes_128_cbc(iv, pkcs7_pad(plaintext, block_size), key)
def test_pkcs7_pad(): assert set2.pkcs7_pad(b"YELLOW SUBMARINE", 20) == b"YELLOW SUBMARINE\x04\x04\x04\x04", \ "Should have four bytes of padding"
def test_pkcs7_pad_max_padding(): assert set2.pkcs7_pad(b"YELLOW SUBMARINES", 16) == \ b"YELLOW SUBMARINES\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f", \ "Should have 15 bytes of padding"
def test_pkcs7_pad_no_padding(): assert set2.pkcs7_pad(b"YELLOW SUBMARINE", 16) == b"YELLOW SUBMARINE", \ "Should have no padding because input is same length as block"
def test_challenge9(): assert set2.pkcs7_pad(b"YELLOW SUBMARINE", 20) == b"YELLOW SUBMARINE\x04\x04\x04\x04" for i in range(100): bytestring = os.urandom(i) assert bytestring == set2.pkcs7_unpad(set2.pkcs7_pad(bytestring, 16), 16)
def generate_payload_second_step(): payload = "" payload += "A" * (block_size - len("email=")) payload += pkcs7_pad(b"admin", block_size).decode() return payload
def encrypt_encoded_user_cookie(user): return encrypt_aes_128_ecb( pkcs7_pad(user.encode_as_cookie().encode(), block_size), global_aes_128_key)