Caesar Cipher is a simple way of encryption that shifts a character a number of places in front of it. For example, ABC with a Rotation of 1 would be BCD. In this program, however, the list of all characters include special characters in a random order, i.e.
1234567890-=qwertyuiopasdfghjklzxcvbnm,./~!@#$%^&*()_+|\\{}[]:;\'"QWERTYUIOPASDFGHJKLZXCVBNM
Thus, it is more of a custom cipher but following the principle of Caesar Cipher, the code can be tested by using the 2 functions I have made (encode() and decode()) on the same text, and if the output given is the same as the input, it means that it works. I get an output for some rotation numbers, but some numbers, like 70, give me an error. The code I have written is:
characters = '`1234567890-=qwertyuiopasdfghjklzxcvbnm,./~!@#$%^&*()_+|\\{}[]:;\'"QWERTYUIOPASDFGHJKLZXCVBNM' # All characters in a string, no specific order
key_raw = 70 # One of the keys that gives me an error.
def encode(text, key): # Function that takes in two inputs: Text and key, which is the number of characters to shift forward
output, text = '', str(text)
limit = len(characters)
while key > limit:
key -= limit # This is my attempt to simplify the key
for i in text:
if i in characters:
output += characters[characters.index(i)+key] # If i is in characters, the rotated character is concatenated to the output
else:
output += i # Otherwise, it directly adds the text
return output
def decode(text, key): # Same thing, except key is subtracted from the index of i in key, as to decrypt it
output, text = '', str(text)
limit = len(characters)
while key > limit:
key -= limit
for i in text:
if i in characters:
output += characters[characters.index(i)-key]
else:
output += i
return output
print(encode('Why do I get String_Index_Out_Of_Range?', key_raw))
Please let me know where I made an error.