I am familiar with Java and Python.
We take the plaintext as p and ciphertext as c which we produce from plaintext during encryption and we produce the plaintext back from the ciphertext using the decryption steps. We encrypt the plaintext p of arbitrary length using key stream generated from key k of arbitrary length.
such that c_i = (p_i + k_i) mod 26 where k_i = key_{i mod m} where m is the length of the key. The pseudo-code is represented as:
m <-- length of key
for index, character in plaintext:
ciphertext[index] <-- (character + key[index % m]) % 26
return ciphertext
This encryption algorithm can be represented in Python as
def encrypt(plaintext: str, key: str) -> str:
ciphertext = ''
m = len(key)
for i, letter in enumerate(plaintext.lower()):
ciphertext += chr((char_2_num(letter) + char_2_num(key[i % m])) % 26 + ord('a'))
return ciphertext.upper()
def char_2_num(character: str) -> int:
return ord(character.lower()) - ord('a')
where char_2_num
is a utility function. Similarly we decrypt the ciphertext using the key by the
following pseudo-code:
m = length of key
for index, character in ciphertext:
plaintext[i] <-- (character - key[index % m]) % 26
return plaintext
The algorithm in Python is represented as:
def decrypt(ciphertext: str, key: str) -> str:
plaintext = ''
m = len(key)
for index, letter in enumerate(ciphertext.lower()):
plaintext += chr(
(char_2_num(letter) - char_2_num(key[index % m])) % 26 + ord('a'))
return plaintext.lower()