Encrypts the plaintext using the Running Key Cipher. :param key: The running key (long piece of text). :param plaintext: The plaintext to be encrypted. :return: The ciphertext.
(key: str, plaintext: str)
| 4 | |
| 5 | |
| 6 | def running_key_encrypt(key: str, plaintext: str) -> str: |
| 7 | """ |
| 8 | Encrypts the plaintext using the Running Key Cipher. |
| 9 | |
| 10 | :param key: The running key (long piece of text). |
| 11 | :param plaintext: The plaintext to be encrypted. |
| 12 | :return: The ciphertext. |
| 13 | """ |
| 14 | plaintext = plaintext.replace(" ", "").upper() |
| 15 | key = key.replace(" ", "").upper() |
| 16 | key_length = len(key) |
| 17 | ciphertext = [] |
| 18 | ord_a = ord("A") |
| 19 | |
| 20 | for i, char in enumerate(plaintext): |
| 21 | p = ord(char) - ord_a |
| 22 | k = ord(key[i % key_length]) - ord_a |
| 23 | c = (p + k) % 26 |
| 24 | ciphertext.append(chr(c + ord_a)) |
| 25 | |
| 26 | return "".join(ciphertext) |
| 27 | |
| 28 | |
| 29 | def running_key_decrypt(key: str, ciphertext: str) -> str: |
no test coverage detected