Write cbc_pad(plaintext_len, block_size), returning the pad length and total blocks, and check it against (742, 20), (760, 20), (1000, 16) and (16, 16).
Write cbc_pad(plaintext_len, block_size), returning the pad length and total blocks, and check it against (742, 20), (760, 20), (1000, 16) and (16, 16).
Answer
def cbc_pad(plaintext_len, block_size): remainder = plaintext_len % block_size pad = block_size - remainder total = plaintext_len + pad return pad, total, total // block_size for n, bb in ((742, 20), (760, 20), (1000, 16), (16, 16)): pad, total, blocks = cbc_pad(n, bb) print(f'{n:>5} bytes / {bb:>2}-byte blocks -> pad {pad:>2}, ciphertext {total:>5} bytes = {blocks:>2} blocks')
SB5e ch2 §2.2; ch20 §20.5; end-of-chapter problems 20.11–20.13