Python-File-Encryptor icon indicating copy to clipboard operation
Python-File-Encryptor copied to clipboard

It's better to use PKCS7 instead of Zero Padding

Open pk5ls20 opened this issue 2 years ago • 0 comments

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

pk5ls20 avatar Feb 24 '23 16:02 pk5ls20