Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pk5ls20 opened this issue Feb 24, 2023 · 0 comments
Open

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

pk5ls20 opened this issue Feb 24, 2023 · 0 comments

Comments

@pk5ls20
Copy link

pk5ls20 commented Feb 24, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant