-
Notifications
You must be signed in to change notification settings - Fork 61
Refactor key generation and file encryption methods #1920
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
Open
lizhiming454
wants to merge
1
commit into
dwebagents:main
Choose a base branch
from
lizhiming454:patch-1
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.
+163
−21
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 hidden or 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 |
|---|---|---|
| @@ -1,63 +1,205 @@ | ||
| """ | ||
| encrypt_decrypt_module.py | ||
| ========================= | ||
| A simple rotation cipher (Caesar cipher variant) module for encrypting | ||
| and decrypting text messages and files. | ||
|
|
||
| How it works: | ||
| - Each alphabetic character is shifted forward (encrypt) or backward | ||
| (decrypt) by a fixed numeric key in the 26-letter alphabet. | ||
| - Non-alphabetic characters (digits, spaces, punctuation) are passed | ||
| through unchanged. | ||
| - Upper- and lower-case letters are handled independently so that | ||
| case is preserved in the output. | ||
|
|
||
| Usage example: | ||
| key = 3 | ||
| cipher = RotateCipher(key) | ||
| encrypted = cipher.encrypt("Hello, World!") # => "Khoor, Zruog!" | ||
| original = cipher.decrypt(encrypted) # => "Hello, World!" | ||
| """ | ||
|
|
||
| import os | ||
| import random | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Key generation | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def generate_key(length): | ||
| # Generate a random key for rotation cipher | ||
| return ''.join(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') for _ in range(length)) | ||
| """Generate a random alphabetic key string for use with RotateCipher. | ||
|
|
||
| The key is composed of randomly selected upper- and lower-case ASCII | ||
| letters. Although RotateCipher only uses the *integer* shift value | ||
| stored in ``self.key``, this helper can produce a human-readable key | ||
| token that a caller could later convert to an integer (e.g. via | ||
| ``sum(ord(c) for c in key) % 26``). | ||
|
|
||
| Args: | ||
| length (int): Number of characters in the generated key string. | ||
|
|
||
| Returns: | ||
| str: A random alphabetic string of the requested length. | ||
|
|
||
| Example: | ||
| >>> k = generate_key(8) | ||
| >>> len(k) | ||
| 8 | ||
| """ | ||
| # Build the key by drawing one random letter at a time | ||
| return ''.join( | ||
| random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') | ||
| for _ in range(length) | ||
| ) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Rotation cipher class | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class RotateCipher: | ||
| """A Caesar-style rotation cipher that shifts letters by a fixed key. | ||
|
|
||
| Attributes: | ||
| key (int): The number of positions each letter is rotated. | ||
| Positive values shift forward (A->D for key=3); | ||
| negative values shift backward. | ||
|
|
||
| Example: | ||
| >>> cipher = RotateCipher(13) # ROT-13 | ||
| >>> cipher.encrypt("Hello") | ||
| 'Uryyb' | ||
| >>> cipher.decrypt("Uryyb") | ||
| 'Hello' | ||
| """ | ||
|
|
||
| def __init__(self, key): | ||
| """Initialise the cipher with the given rotation key. | ||
|
|
||
| Args: | ||
| key (int): Rotation amount (number of alphabet positions to shift). | ||
| """ | ||
| self.key = key | ||
|
|
||
| def encrypt(self, message): | ||
| """Encrypt a plaintext message by rotating each letter forward. | ||
|
|
||
| Non-alphabetic characters are copied to the output unchanged. | ||
| Case is preserved: upper-case letters remain upper-case, and | ||
| lower-case letters remain lower-case after the shift. | ||
|
|
||
| Args: | ||
| message (str): The plaintext string to encrypt. | ||
|
|
||
| Returns: | ||
| str: The ciphertext string with each letter shifted by ``self.key``. | ||
|
|
||
| Example: | ||
| >>> RotateCipher(3).encrypt("abc XYZ!") | ||
| 'def ABC!' | ||
| """ | ||
| encrypted = "" | ||
| for char in message: | ||
| if char.isalpha(): | ||
| # Choose the correct ASCII base depending on letter case | ||
| ascii_offset = ord('A') if char.isupper() else ord('a') | ||
| # Rotate forward within the 26-letter alphabet (mod 26 wraps around) | ||
| new_char = chr((ord(char) - ascii_offset + self.key) % 26 + ascii_offset) | ||
| encrypted += new_char | ||
| else: | ||
| # Non-alphabetic characters are kept as-is | ||
| encrypted += char | ||
| return encrypted | ||
|
|
||
| def decrypt(self, message): | ||
| """Decrypt a ciphertext message by rotating each letter backward. | ||
|
|
||
| This is the exact inverse of :meth:`encrypt`. Non-alphabetic | ||
| characters are passed through unchanged and letter case is preserved. | ||
|
|
||
| Args: | ||
| message (str): The ciphertext string to decrypt. | ||
|
|
||
| Returns: | ||
| str: The recovered plaintext string. | ||
|
|
||
| Example: | ||
| >>> RotateCipher(3).decrypt("def ABC!") | ||
| 'abc XYZ!' | ||
| """ | ||
| decrypted = "" | ||
| for char in message: | ||
| if char.isalpha(): | ||
| # Choose the correct ASCII base depending on letter case | ||
| ascii_offset = ord('A') if char.isupper() else ord('a') | ||
| # Rotate backward within the 26-letter alphabet (mod 26 wraps around) | ||
| new_char = chr((ord(char) - ascii_offset - self.key) % 26 + ascii_offset) | ||
| decrypted += new_char | ||
| else: | ||
| # Non-alphabetic characters are kept as-is | ||
| decrypted += char | ||
| return decrypted | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # File-level helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def encrypt_file(input_filename, output_filename): | ||
| """Read a plaintext file, encrypt its contents, and write to a new file. | ||
|
|
||
| A fresh random key is generated for every call, so the same input file | ||
| will produce different ciphertext each time. The key is printed to | ||
| stdout so the caller can record it for later decryption. | ||
|
|
||
| Args: | ||
| input_filename (str): Path to the plaintext source file. | ||
| output_filename (str): Path where the encrypted output will be written. | ||
|
|
||
| Side effects: | ||
| Prints the generated key to stdout. | ||
| Creates (or overwrites) ``output_filename``. | ||
|
|
||
| Example: | ||
| >>> encrypt_file("secret.txt", "secret.enc") | ||
| Key: 7 | ||
| """ | ||
| # Read the entire source file | ||
| with open(input_filename, 'r') as file: | ||
| message = file.read() | ||
|
|
||
| key = generate_key(len(message)) | ||
|
|
||
| # Generate a new random integer key in the range [1, 25] | ||
| key = random.randint(1, 25) | ||
| print(f"Key: {key}") | ||
|
Collaborator
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. thanks, usually it's a good idea to print encryption keys to stdout like this. |
||
|
|
||
| # Encrypt and write the ciphertext to the output file | ||
| cipher = RotateCipher(key) | ||
| encrypted_message = cipher.encrypt(message) | ||
|
|
||
| with open(output_filename, 'w') as file: | ||
| file.write(encrypted_message) | ||
|
|
||
| def decrypt_file(input_filename, output_filename): | ||
|
|
||
| def decrypt_file(input_filename, output_filename, key): | ||
| """Read an encrypted file, decrypt its contents, and write to a new file. | ||
|
|
||
| Args: | ||
| input_filename (str): Path to the encrypted source file. | ||
| output_filename (str): Path where the decrypted output will be written. | ||
| key (int): The rotation key that was used during encryption. | ||
|
|
||
| Side effects: | ||
| Creates (or overwrites) ``output_filename``. | ||
|
|
||
| Example: | ||
| >>> decrypt_file("secret.enc", "recovered.txt", 7) | ||
| """ | ||
| # Read the entire encrypted file | ||
| with open(input_filename, 'r') as file: | ||
| message = file.read() | ||
| key = generate_key(len(message)) | ||
|
|
||
| # Decrypt using the provided key and write the result | ||
| cipher = RotateCipher(key) | ||
| decrypted_message = cipher.decrypt(message) | ||
|
|
||
| with open(output_filename, 'w') as file: | ||
| file.write(decrypted_message) | ||
|
|
||
| def main(): | ||
| encrypt_file('original.txt', 'encrypted.txt') | ||
| decrypt_file('encrypted.txt', 'decrypted.txt') | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
| # Klingon poetry | ||
| # Vek'tal na kai'men, na'kathro, u'tar'gat. | ||
|
Comment on lines
-62
to
-63
Collaborator
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. please do not vandalize the artwork |
||
Oops, something went wrong.
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.
cool, it was always unclear whether the key was an int or a string anyway. 5 bits of insecure entropy is probably plenty.