425nesp
425nesp

Reputation: 7583

Implement PKCS #7 Padding Scheme for AES in Python

I've written a small command line utility to encrypt single files with AES, using Python 3. As I'm sure we all know, AES works on 16-byte blocks, so if I want to encrypt a file that isn't exactly a multiple of 16, then I'll have to pad the file to make it a multiple of 16. PKCS #7 padding scheme says that I should pad the last chunk with N bytes all of value N. This is how I do that in my encryption function.

for chunk in getChunks(plainFile, chunkSizeBytes):
    padLength = ((AES.block_size - len(chunk)) % AES.block_size)
    # We have to have padding!
    if padLength == 0:
        padLength = 16
    pad = chr(padLength) * padLength
    chunk += pad.encode('utf-8')

    # Write the encrypted chunk to an output file.
    cipherFile.write(en.encrypt(chunk))

However, I'm unsure about how I should read this data from that last chunk of a decrypted file. Is there a way to read in files in reverse order? What's the correct way to do this?

Upvotes: 2

Views: 2153

Answers (1)

Quentin Pradet
Quentin Pradet

Reputation: 4771

I should pad the last chunk with N bytes all of value N.

In this sentence, the first N is equal to the second N, which means the value of the byte determines how much characters you need to remove for decoding.

For example, if you only have 9 characters in your last chunk, pad with 7 characters of value 7 (7 turns out to be the BEL character, but that doesn't matter).

Upvotes: 1

Related Questions