codekingpro/portable-devtools
114k
1# Copyright (c) 2023, exiledkingcc2# All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions are6# met:7#8# * Redistributions of source code must retain the above copyright notice,9# this list of conditions and the following disclaimer.10# * Redistributions in binary form must reproduce the above copyright notice,11# this list of conditions and the following disclaimer in the documentation12# and/or other materials provided with the distribution.13# * The name of the author may not be used to endorse or promote products14# derived from this software without specific prior written permission.15#16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE26# POSSIBILITY OF SUCH DAMAGE.27 28from pypdf._crypt_providers._base import CryptBase29from pypdf.errors import DependencyError30 31_DEPENDENCY_ERROR_STR = "cryptography>=3.1 is required for AES algorithm"32 33 34crypt_provider = ("local_crypt_fallback", "0.0.0")35 36 37class CryptRC4(CryptBase):38 def __init__(self, key: bytes) -> None:39 self.s = bytearray(range(256))40 j = 041 for i in range(256):42 j = (j + self.s[i] + key[i % len(key)]) % 25643 self.s[i], self.s[j] = self.s[j], self.s[i]44 45 def encrypt(self, data: bytes) -> bytes:46 s = bytearray(self.s)47 out = [0 for _ in range(len(data))]48 i, j = 0, 049 for k in range(len(data)):50 i = (i + 1) % 25651 j = (j + s[i]) % 25652 s[i], s[j] = s[j], s[i]53 x = s[(s[i] + s[j]) % 256]54 out[k] = data[k] ^ x55 return bytes(out)56 57 def decrypt(self, data: bytes, *, strict: bool = True) -> bytes:58 return self.encrypt(data)59 60 61class CryptAES(CryptBase):62 def __init__(self, key: bytes) -> None:63 pass64 65 def encrypt(self, data: bytes) -> bytes:66 raise DependencyError(_DEPENDENCY_ERROR_STR)67 68 def decrypt(self, data: bytes, *, strict: bool = True) -> bytes:69 raise DependencyError(_DEPENDENCY_ERROR_STR)70 71 72def rc4_encrypt(key: bytes, data: bytes) -> bytes:73 return CryptRC4(key).encrypt(data)74 75 76def rc4_decrypt(key: bytes, data: bytes) -> bytes:77 return CryptRC4(key).decrypt(data)78 79 80def aes_ecb_encrypt(key: bytes, data: bytes) -> bytes:81 raise DependencyError(_DEPENDENCY_ERROR_STR)82 83 84def aes_ecb_decrypt(key: bytes, data: bytes) -> bytes:85 raise DependencyError(_DEPENDENCY_ERROR_STR)86 87 88def aes_cbc_encrypt(key: bytes, iv: bytes, data: bytes) -> bytes:89 raise DependencyError(_DEPENDENCY_ERROR_STR)90 91 92def aes_cbc_decrypt(key: bytes, iv: bytes, data: bytes) -> bytes:93 raise DependencyError(_DEPENDENCY_ERROR_STR)94 