Skip to content

It's better to use PKCS7 instead of Zero Padding #7

Open
@pk5ls20

Description

@pk5ls20

When use Zero Padding encrypt and decrypt, some types of files (such as .7z file) which ends with bytes '00' will damaged file.
屏幕截图 2023-02-25 001502
I have encountered and reproduced this problem, and I use PKCS7 instead of Zero Padding and it worked.
I modified:

    def pad(self, s):
        pad_len = AES.block_size - len(s) % AES.block_size
        padding = bytes([pad_len] * pad_len)
        return s + padding

    def decrypt(self, ciphertext, key):
        iv = ciphertext[:AES.block_size]
        cipher = AES.new(key, AES.MODE_CBC, iv)
        plaintext = cipher.decrypt(ciphertext[AES.block_size:])
        pad_len = plaintext[-1]
        return plaintext[:-pad_len]

If you update the code as I did, please be sure to indicate in README.MD that you have modified the code, otherwise users may not be able to use the updated code to decrypt the content encrypted by the old code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pk5ls20

        Issue actions

          It's better to use PKCS7 instead of Zero Padding · Issue #7 · the-javapocalypse/Python-File-Encryptor