Caesar Cipher
- Try to implement caesar cipher using plain python.
def cipher(a_string, key):
uppercase = ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowercase = abcdefghijklmnopqrstuvwxyz
encrypt = ''
for x in a_string:
if x in uppercase:
print(uppercase.index(x))
# (1)
new = (uppercase.index(x) + key) % 26
# (2)
encrypt += uppercase[new]
elif x in lowercase:
print(lowercase.index(x))
new = (lowercase.index(x) + key) % 26
encrypt += lowercase[new]
else:
encrypt += x
print("Cipher text: ", encrypt)
return encrypt
a_string = input("Enter a name")
key = input("Enter a key (must be integer) ")
cipher(a_string, int(key))
How it works #
- (1) if the letter is uppercase we get index of it and add key to it.
- (1.1) and then we use modulus operator(which return either 0 or remainder) with 26 as divisor
- (1.2) we treat new as new index of letter
- (2) then we append to encrypt variable with the new index of letter.
Can’t understand what i wrote here? #
- Go ahead and look it on internet
- here some links to resources
- Caesar Cipher
- Modolus Operator