From f804f3af83b1856492ea94e537de1c5350585add Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 10 Dec 2022 09:16:02 +0100 Subject: [PATCH] ROB: Padding issue with AES encryption (#1469) Fixes #1221 Credit goes to Alper Ahmetoglu for the fix Co-authored-by: Alper Ahmetoglu --- PyPDF2/_encryption.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PyPDF2/_encryption.py b/PyPDF2/_encryption.py index 2b0e15328..b07211ea8 100644 --- a/PyPDF2/_encryption.py +++ b/PyPDF2/_encryption.py @@ -58,6 +58,7 @@ class CryptIdentity(CryptBase): try: from Crypto.Cipher import AES, ARC4 # type: ignore[import] + from Crypto.Util.Padding import pad # type: ignore[import] class CryptRC4(CryptBase): def __init__(self, key: bytes) -> None: @@ -84,6 +85,8 @@ def decrypt(self, data: bytes) -> bytes: iv = data[:16] data = data[16:] aes = AES.new(self.key, AES.MODE_CBC, iv) + if len(data) % 16: + data = pad(data, 16) d = aes.decrypt(data) if len(d) == 0: return d