MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / running_key_decrypt

Function running_key_decrypt

ciphers/running_key_cipher.py:29–49  ·  view source on GitHub ↗

Decrypts the ciphertext using the Running Key Cipher. :param key: The running key (long piece of text). :param ciphertext: The ciphertext to be decrypted. :return: The plaintext.

(key: str, ciphertext: str)

Source from the content-addressed store, hash-verified

27
28
29def running_key_decrypt(key: str, ciphertext: str) -> str:
30 """
31 Decrypts the ciphertext using the Running Key Cipher.
32
33 :param key: The running key (long piece of text).
34 :param ciphertext: The ciphertext to be decrypted.
35 :return: The plaintext.
36 """
37 ciphertext = ciphertext.replace(" ", "").upper()
38 key = key.replace(" ", "").upper()
39 key_length = len(key)
40 plaintext = []
41 ord_a = ord("A")
42
43 for i, char in enumerate(ciphertext):
44 c = ord(char) - ord_a
45 k = ord(key[i % key_length]) - ord_a
46 p = (c - k) % 26
47 plaintext.append(chr(p + ord_a))
48
49 return "".join(plaintext)
50
51
52def test_running_key_encrypt() -> None:

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected