-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create vernam_cipher.py #10702
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
Merged
Merged
Create vernam_cipher.py #10702
Changes from 8 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
8ce27f1
Create vernam_cipher.py
Anupamaraie f5e8dff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c8aae7e
Update vernam_cipher.py
Anupamaraie 44efe07
Update vernam_cipher.py
Anupamaraie 94f6768
Update vernam_cipher.py
Anupamaraie 7e19ac6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f25c0d0
Update vernam_cipher.py
Anupamaraie ee52bdf
Update vernam_cipher.py
Anupamaraie 5955904
Update vernam_cipher.py
Anupamaraie 7d8e29b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a0e9cf5
Update vernam_cipher.py
Anupamaraie 64ebf31
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 58b9743
Update vernam_cipher.py
Anupamaraie 720df35
Update vernam_cipher.py
Anupamaraie a5a6798
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0aef0ff
Update vernam_cipher.py
Anupamaraie c2aa8d4
Update vernam_cipher.py
Anupamaraie 2181ab0
Update vernam_cipher.py
Anupamaraie 58581b4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a92771e
Update vernam_cipher.py
Anupamaraie fa85233
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0dcffec
Update vernam_cipher.py
Anupamaraie b5cc608
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ce28b1c
Update vernam_cipher.py
Anupamaraie 0963c95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c1d6adb
Update vernam_cipher.py
Anupamaraie 2a8db25
Update vernam_cipher.py
Anupamaraie fb59599
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 322854c
Update vernam_cipher.py
Anupamaraie 88c87cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ff89e78
Update vernam_cipher.py
Anupamaraie 3901e65
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c1d504f
Update vernam_cipher.py
Anupamaraie d68ac5e
Update vernam_cipher.py
Anupamaraie 33b903c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 68c314b
Update vernam_cipher.py
Anupamaraie 4ce0a29
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] eb4062c
Update vernam_cipher.py
Anupamaraie 7e2346c
Update vernam_cipher.py
Anupamaraie d6bc1d7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bd8ac7f
Update vernam_cipher.py
Anupamaraie 3e5078e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e287109
Update vernam_cipher.py
Anupamaraie 7058daf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8ae9981
Update vernam_cipher.py
Anupamaraie 4016a43
Update ciphers/vernam_cipher.py
cclauss bae9f65
Update vernam_cipher.py
cclauss 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
def vernam_encrypt(plaintext: str, key: str) -> str: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
Anupamaraie marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ciphertext = "" | ||
for i in range(len(plaintext)): | ||
ct = ord(key[i % len(key)]) - 65 + ord(plaintext[i]) - 65 | ||
while ct > 25: | ||
ct = ct - 26 | ||
ciphertext += chr(65 + ct) | ||
""" | ||
>>> vernam_encrypt("HELLO","KEY") | ||
'RIJVS' | ||
""" | ||
return ciphertext | ||
|
||
def vernam_decrypt(ciphertext: str, key: str) -> str: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
Anupamaraie marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decrypted_text = "" | ||
for i in range(len(ciphertext)): | ||
ct = ord(ciphertext[i]) - ord(key[i % len(key)]) | ||
while ct < 0: | ||
ct = 26 + ct | ||
decrypted_text += chr(65 + ct) | ||
""" | ||
>>> vernam_decrypt("RIJVS","KEY") | ||
'HELLO' | ||
""" | ||
return decrypted_text | ||
|
||
|
||
# Example usage | ||
plaintext = input("Enter the message: ").upper() | ||
key = input("Enter the key: ").upper() | ||
encrypted_text = vernam_encrypt(plaintext, key) | ||
decrypted_text = vernam_decrypt(encrypted_text, key) | ||
|
||
print("\n\n") | ||
print("Plaintext:", plaintext) | ||
print("Encrypted:", encrypted_text) | ||
print("Decrypted:", decrypted_text) | ||
|
||
# Tests | ||
|
||
#encrypt_txt = vernam_encrypt("HELLO", "KEY") | ||
#print(encrypt_txt) | ||
|
||
#decrypt_txt = vernam_decrypt(encrypt_txt, "KEY") | ||
#print(decrypt_txt) |
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.