-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
ragmehos
wants to merge
1
commit into
mhdzumair:main
Choose a base branch
from
ragmehos:compression
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
Comment on lines
37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling and backward compatibility The decompression step lacks error handling and breaks backward compatibility with existing encrypted data.
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")
# 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: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add input validation for IV and ciphertext
The function lacks crucial input validation which could lead to security issues:
Apply this fix:
📝 Committable suggestion