-
- Notifications
You must be signed in to change notification settings - Fork 48.6k
Implement modified vigenere cipher for all printable ASCII #13452
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
Abheelash-Mishra wants to merge 3 commits into TheAlgorithms:master Choose a base branch from Abheelash-Mishra:add_printable_ascii_vigenere_cipher
base: master
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.
+104 −0
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
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
""" | ||
This program encrypts or decrypts messages using a modified Vigenere cipher | ||
that operates on all printable ASCII characters (from 32-126) instead of just letters. | ||
Each character in the message is shifted by a value derived from the corresponding | ||
character in the key. When the end of the key is reached, it repeats cyclically. | ||
| ||
Algorithm summary: | ||
- For each printable character: | ||
* Convert both the message and key characters to their ASCII codes. | ||
* During encryption: add the key's ASCII value to the message character. | ||
* During decryption: subtract the key's ASCII value from the message character. | ||
* Wrap the result within the printable ASCII range using modulo arithmetic. | ||
- Non-printable characters are left unchanged. | ||
- The key repeats automatically to match the message length. | ||
| ||
Reference: https://stackoverflow.com/questions/28182091/ascii-vigenere-cipher-implementation-in-java | ||
""" | ||
| ||
PRINTABLE_START = 32 | ||
PRINTABLE_END = 126 | ||
PRINTABLE_RANGE = PRINTABLE_END - PRINTABLE_START + 1 | ||
| ||
| ||
def encrypt_message(key: str, message: str) -> str: | ||
""" | ||
Encrypts a message using printable ASCII Vigenere cipher. | ||
| ||
>>> encrypt_message('key', 'Hello!') | ||
"Tk'xu;" | ||
>>> encrypt_message('SUpeR$eCR3t_KEY', 'This is a message to encrypt.') | ||
'H^zyr.ycTS#e_Y[[[1zbDkRVF/p`s' | ||
""" | ||
return translate_message(key, message, "encrypt") | ||
| ||
| ||
def decrypt_message(key: str, message: str) -> str: | ||
""" | ||
Decrypts a message using printable ASCII Vigenere cipher. | ||
| ||
>>> decrypt_message('key', "Tk'xu;") | ||
'Hello!' | ||
>>> decrypt_message('SUpeR$eCR3t_KEY', 'H^zyr.ycTS#e_Y[[[1zbDkRVF/p`s') | ||
'This is a message to encrypt.' | ||
""" | ||
return translate_message(key, message, "decrypt") | ||
| ||
| ||
def translate_message(key: str, message: str, mode: str) -> str: | ||
""" | ||
Translates (encrypts or decrypts) a message using the given key. | ||
| ||
>>> translate_message('key', 'Hello!', 'encrypt') | ||
"Tk'xu;" | ||
>>> translate_message('key', "Tk'xu;", 'decrypt') | ||
'Hello!' | ||
""" | ||
translated = [] | ||
key_index = 0 | ||
key_bytes = [ord(ch) for ch in key] | ||
| ||
for symbol in message: | ||
sym_val = ord(symbol) | ||
| ||
if PRINTABLE_START <= sym_val <= PRINTABLE_END: | ||
key_val = key_bytes[key_index] | ||
| ||
if mode == "encrypt": | ||
new_val = ( | ||
sym_val - PRINTABLE_START + key_val | ||
) % PRINTABLE_RANGE + PRINTABLE_START | ||
elif mode == "decrypt": | ||
new_val = ( | ||
sym_val - PRINTABLE_START - key_val | ||
) % PRINTABLE_RANGE + PRINTABLE_START | ||
else: | ||
raise ValueError("Mode must be 'encrypt' or 'decrypt'") | ||
| ||
translated.append(chr(new_val)) | ||
| ||
key_index = (key_index + 1) % len(key_bytes) | ||
else: | ||
translated.append(symbol) | ||
| ||
return "".join(translated) | ||
| ||
| ||
if __name__ == "__main__": | ||
message = input("Enter message: ") | ||
key = input("Enter secret key: ") | ||
mode = input("Encrypt/Decrypt [e/d]: ") | ||
| ||
if mode.lower().startswith("e"): | ||
mode = "encrypt" | ||
translated = encrypt_message(key, message) | ||
| ||
print(f"Encrypted message: {translated}") | ||
elif mode.lower().startswith("d"): | ||
mode = "decrypt" | ||
translated = decrypt_message(key, message) | ||
| ||
print(f"Decrypted message: {translated}") | ||
else: | ||
print("Invalid mode. Use 'e' or 'd'.") |
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.
Uh oh!
There was an error while loading. Please reload this page.