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

Create vernam_cipher.py #10702

Merged
merged 47 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
8ce27f1
Create vernam_cipher.py
Anupamaraie Oct 19, 2023
f5e8dff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
c8aae7e
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
44efe07
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
94f6768
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
7e19ac6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
f25c0d0
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
ee52bdf
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
5955904
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
7d8e29b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
a0e9cf5
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
64ebf31
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
58b9743
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
720df35
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
a5a6798
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
0aef0ff
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
c2aa8d4
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
2181ab0
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
58581b4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
a92771e
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
fa85233
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
0dcffec
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
b5cc608
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
ce28b1c
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
0963c95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
c1d6adb
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
2a8db25
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
fb59599
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
322854c
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
88c87cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
ff89e78
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
3901e65
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
c1d504f
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
d68ac5e
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
33b903c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
68c314b
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
4ce0a29
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
eb4062c
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
7e2346c
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
d6bc1d7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
bd8ac7f
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
3e5078e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
e287109
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
7058daf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
8ae9981
Update vernam_cipher.py
Anupamaraie Oct 19, 2023
4016a43
Update ciphers/vernam_cipher.py
cclauss Oct 19, 2023
bae9f65
Update vernam_cipher.py
cclauss Oct 19, 2023
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
42 changes: 42 additions & 0 deletions ciphers/vernam_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def vernam_encrypt(plaintext: str, key: str) -> str:
"""
>>> vernam_encrypt("HELLO","KEY")
'RIJVS'
"""
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)
return ciphertext


def vernam_decrypt(ciphertext: str, key: str) -> str:
"""
>>> vernam_decrypt("RIJVS","KEY")
'HELLO'
"""
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)
return decrypted_text


if __name__ == "__main__":
from doctest import testmod

testmod()

# Example usage
plaintext = "HELLO"
key = "KEY"
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)