cryptopals

Cryptopals Challenges
git clone https://git.pngu.org/cryptopals
Log | Files | Refs

commit 8bdc32650426b580fc9972834fa2e3dd067fbe1e
parent 16c34670eeb091fb40430b3a87b4a0c0b1ffdb99
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue,  7 Oct 2025 22:13:38 -0600

challenge 9: implement pkcs#7 padding

Diffstat:
Asrc/09_implement_pkcs7_padding.c | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/src/09_implement_pkcs7_padding.c b/src/09_implement_pkcs7_padding.c @@ -0,0 +1,23 @@ +// Implement PKCS#7 padding +#include "aes.h" + +/* + * a block cipher transforms a fixed-size block (usually 8 or 16) bytes of + * plaintext into ciphertext. but almost never want to transform a single block; + * we encrypt irregularly sized messages. one way we account for irregularly + * sized messages is by padding, creating a plaintext that is an even multiple + * of the block size. the most popular padding scheme is pkcs#7. so: pad any + * block to a specific length by appending the number of bytes of padding to the + * end of the block. for instance, "YELLOW SUBMARINE" ...padded to 20 bytes + * would be "YELLOW SUBMARINE\x04\x04\x04\x04" + */ + +int main(void) { + static char *input_output = "YELLOW SUBMARINE"; + struct bytes res = pkcs7(input_output, strlen(input_output), 16); + printf("Input: %s\n", input_output); + printf("Output: %s\n", res.data); + printf("Expected: %s\n", input_output); + free(res.data); + return 0; +}