Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34f48b6

Browse files
Anupamaraiepre-commit-ci[bot]cclauss
authored
Create vernam_cipher.py (TheAlgorithms#10702)
* Create vernam_cipher.py added vernam cipher * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py added return type * Update vernam_cipher.py added type hint for plaintext and key * Update vernam_cipher.py added tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py Added tests * Update vernam_cipher.py * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * Update vernam_cipher.py * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update vernam_cipher.py * Update ciphers/vernam_cipher.py * Update vernam_cipher.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent be94690 commit 34f48b6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎ciphers/vernam_cipher.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def vernam_encrypt(plaintext: str, key: str) -> str:
2+
"""
3+
>>> vernam_encrypt("HELLO","KEY")
4+
'RIJVS'
5+
"""
6+
ciphertext = ""
7+
for i in range(len(plaintext)):
8+
ct = ord(key[i % len(key)]) - 65 + ord(plaintext[i]) - 65
9+
while ct > 25:
10+
ct = ct - 26
11+
ciphertext += chr(65 + ct)
12+
return ciphertext
13+
14+
15+
def vernam_decrypt(ciphertext: str, key: str) -> str:
16+
"""
17+
>>> vernam_decrypt("RIJVS","KEY")
18+
'HELLO'
19+
"""
20+
decrypted_text = ""
21+
for i in range(len(ciphertext)):
22+
ct = ord(ciphertext[i]) - ord(key[i % len(key)])
23+
while ct < 0:
24+
ct = 26 + ct
25+
decrypted_text += chr(65 + ct)
26+
return decrypted_text
27+
28+
29+
if __name__ == "__main__":
30+
from doctest import testmod
31+
32+
testmod()
33+
34+
# Example usage
35+
plaintext = "HELLO"
36+
key = "KEY"
37+
encrypted_text = vernam_encrypt(plaintext, key)
38+
decrypted_text = vernam_decrypt(encrypted_text, key)
39+
print("\n\n")
40+
print("Plaintext:", plaintext)
41+
print("Encrypted:", encrypted_text)
42+
print("Decrypted:", decrypted_text)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /