This is a simple plug-and-play ROT function. Simply pass the cipher function a string, key, shift value, and mode (1 for encipher, and -1 for decipher) and the function will return a enciphered / deciphered string.
It is possible to use any key and any shift value with this function. If a character is not found in the key, it is just added to the final output string.
# the symmetric key used to encipher / decipher characters key = "abcdefghijklmnopqrstuvwxyz" # how many places to shift each letter in the key shift = 13 # 1 = encipher / -1 = decipher mode = 1 def cipher(string, key, shift, mode): # initialise output list output =  for char in string: # encipher / decipher the character only if it is in the key if char in key: # the pointer is equal to the position of the character in the key plus the shift value multiplied by the mode pointer = key.index(char) + shift * mode # append the value at the position of the pointer modulus the length of the key (wrap around) output.append(key[pointer % len(key)]) # if the character is not in the key just append it to the output list else: output.append(char) # concatenate all characters in the output list and return as a string return ''.join(output) print(cipher("william", key, shift, mode))
key = "abcdefghijklmnopqrstuvwxyz" # the symmetric key used to encipher / decipher characters shift = 13 # how many places to shift each letter in the key mode = 1 # 1 = encipher / -1 = decipher ... print(cipher("hello world", key, shift, mode)) # Returns "uryyb jbeyq"
Thank you for reading.🚀🚀🚀