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

Change compression prior to encryption for best results #351

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions utils/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Comment on lines +32 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add input validation for IV and ciphertext

The function lacks crucial input validation which could lead to security issues:

  1. No validation of IV length (should be 16 bytes)
  2. No validation of decoded data length (should be at least IV length + block size)

Apply this fix:

 decoded_data = urlsafe_b64decode(secret_str)
+ if len(decoded_data) < 32:  # minimum length: 16 (IV) + 16 (one AES block)
+     raise ValueError("Invalid encrypted data length")
 iv = decoded_data[:16]
 if isinstance(secret_key, str):
     secret_key = secret_key.encode("utf-8")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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:])
decoded_data = urlsafe_b64decode(secret_str)
if len(decoded_data) < 32: # minimum length: 16 (IV) + 16 (one AES block)
raise ValueError("Invalid encrypted data length")
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(decoded_data[16:])

decrypted_data = decrypted_data.rstrip(b"\0")
return decrypted_data.decode("utf-8")
return zlib.decompress(decrypted_data).decode("utf-8")
Comment on lines 37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling and backward compatibility

The decompression step lacks error handling and breaks backward compatibility with existing encrypted data.

  1. Add error handling for decompression:
 decrypted_data = decrypted_data.rstrip(b"\0")
- return zlib.decompress(decrypted_data).decode("utf-8")
+ try:
+     return zlib.decompress(decrypted_data).decode("utf-8")
+ except zlib.error:
+     # Attempt backward compatibility with uncompressed data
+     try:
+         return decrypted_data.decode("utf-8")
+     except UnicodeDecodeError:
+         raise ValueError("Failed to decrypt: invalid data format")
  1. Consider adding a version byte to the encrypted data to properly handle both compressed and uncompressed formats:
# In encrypt_text:
version = b'\x01'  # Version 1: compressed
encrypted_str = urlsafe_b64encode(version + iv + encrypted_data).decode("utf-8")

# In decrypt_text:
version = decoded_data[0:1]
iv = decoded_data[1:17]
if version == b'\x01':
    # Handle compressed data
else:
    # Handle uncompressed data



def encrypt_user_data(user_data: UserData) -> str:
Expand Down