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)
| 27 | |
| 28 | |
| 29 | def 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 | |
| 52 | def test_running_key_encrypt() -> None: |
no test coverage detected