mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Create vernam_cipher.py (#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>
This commit is contained in:
parent
be94690dec
commit
34f48b684b
42
ciphers/vernam_cipher.py
Normal file
42
ciphers/vernam_cipher.py
Normal file
|
@ -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)
|
Loading…
Reference in New Issue
Block a user