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

Function running_key_encrypt

ciphers/running_key_cipher.py:6–26  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

4
5
6def 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
29def running_key_decrypt(key: str, ciphertext: str) -> str:

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected