diff --git a/utils/crypto.py b/utils/crypto.py index 81ef793d..4ae16bb6 100644 --- a/utils/crypto.py +++ b/utils/crypto.py @@ -19,25 +19,23 @@ def encrypt_text(text: str, secret_key: str | bytes) -> str: if isinstance(secret_key, str): secret_key = secret_key.encode("utf-8") cipher = AES.new(secret_key.ljust(32)[:32], AES.MODE_CBC, iv) - encoded_text = text.encode("utf-8") + encoded_text = zlib.compress(text.encode("utf-8")) encrypted_data = cipher.encrypt( encoded_text + b"\0" * (16 - len(encoded_text) % 16) ) - compressed_data = zlib.compress(iv + encrypted_data) - encrypted_str = urlsafe_b64encode(compressed_data).decode("utf-8") + encrypted_str = urlsafe_b64encode(iv + encrypted_data).decode("utf-8") return encrypted_str def decrypt_text(secret_str: str, secret_key: str | bytes) -> str: decoded_data = urlsafe_b64decode(secret_str) - encrypted_data = zlib.decompress(decoded_data) - iv = encrypted_data[:16] + iv = decoded_data[:16] if isinstance(secret_key, str): secret_key = secret_key.encode("utf-8") cipher = AES.new(secret_key.ljust(32)[:32], AES.MODE_CBC, iv) - decrypted_data = cipher.decrypt(encrypted_data[16:]) + decrypted_data = cipher.decrypt(decoded_data[16:]) decrypted_data = decrypted_data.rstrip(b"\0") - return decrypted_data.decode("utf-8") + return zlib.decompress(decrypted_data).decode("utf-8") def encrypt_user_data(user_data: UserData) -> str: