I am working on some tasks in Crypto, specifically on this small task: https://cryptopals.com/sets/2/challenges/9, which I aim to implement in Python.
The point is to add padding for a certain number of bytes, and the value of each byte is the number of bytes added. The example given is "YELLOW SUBMARINE\x04\x04\x04\x04".
I have solved this task I think, but the output is non-displayable. Here is one clean solution I found online
def pad_pkcs7(buffer, block_size): if len(buffer) % block_size: padding = (len(buffer) / block_size + 1) * block_size - len(buffer) else: padding = 0 # Padding size must be less than a byte assert 0 <= padding <= 255 new_buffer = bytearray() new_buffer[:] = buffer new_buffer += bytearray([chr(padding)] * padding) return new_buffer buffer = bytearray("YELLOW SUBMARINEX") print str(pad_pkcs7(buffer, 20))
When I run this I get the same result as with my own solution: YELLOW SUBMARINE[?][?][?][?] (the s are actually boxes with question marks in them)
- Why is the padding hex encoded in the first place? Why not YELLOW SUBMARINE4444? Odd that it is part ASCII and part hex
- Did this solution actually solve the problem? Should not the hex-encoded version of 4 be quite possible to display as 4, 04, \x04?