caesar cipher python ascii shift decrypt code example
Example 1: python ascii caesar cipher
def ascii_caesar_shift(message, distance):
encrypted = ""
for char in message:
value = ord(char) + distance
encrypted += chr(value % 128)
return encrypted
Example 2: python caesar cipher
plaintext = input("Please enter your plaintext: ")
shift = input("Please enter your key: ")
alphabet = "abcdefghijklmnopqrstuvwxyz"
ciphertext = ""
while isinstance(int(shift), int) == False:
shift = input("Please enter your key (integers only!): ")
shift = int(shift)
new_ind = 0
for i in plaintext:
if i.lower() in alphabet:
new_ind = alphabet.index(i) + shift
ciphertext += alphabet[new_ind % 26]
else:
ciphertext += i
print("The ciphertext is: " + ciphertext)