1def ascii_caesar_shift(message, distance):
2 encrypted = ""
3 for char in message:
4 value = ord(char) + distance
5 encrypted += chr(value % 128) #128 for ASCII
6 return encrypted
1def caesar_encrypt():
2 word = input('Enter the plain text: ')
3 c = ''
4 for i in word:
5 if (i == ' '):
6 c += ' '
7 else:
8 c += (chr(ord(i) + 3))
9 return c
10
11def caesar_decrypt():
12 word = input('Enter the cipher text: ')
13 c = ''
14 for i in word:
15 if (i == ' '):
16 c += ' '
17 else:
18 c += (chr(ord(i) - 3))
19 return c
20
21plain = 'hello'
22cipher = caesar_encrypt(plain)
23decipher = caesar_decrypt(cipher)
24
1plaintext = input("Please enter your plaintext: ")
2shift = input("Please enter your key: ")
3alphabet = "abcdefghijklmnopqrstuvwxyz"
4ciphertext = ""
5
6# shift value can only be an integer
7while isinstance(int(shift), int) == False:
8 # asking the user to reenter the shift value
9 shift = input("Please enter your key (integers only!): ")
10
11shift = int(shift)
12
13new_ind = 0 # this value will be changed later
14for i in plaintext:
15 if i.lower() in alphabet:
16 new_ind = alphabet.index(i) + shift
17 ciphertext += alphabet[new_ind % 26]
18 else:
19 ciphertext += i
20print("The ciphertext is: " + ciphertext)
1def cc_encrypt(msg: str,key: int) -> str:
2 encrypted_msg = ''
3
4 try:
5 for char in msg:
6 encrypted_msg += str(chr(ord(char)+int(key)))
7 except:
8 print(Exception)
9 pass
10
11 return encrypted_msg
12
13def cc_decrypt(msg: str,key: int) -> str:
14 decrypted_msg = ''
15
16 try:
17 for char in msg:
18 decrypted_msg += chr(ord(char)-int(key))
19 except:
20 print(Exception)
21 pass
22
23 return decrypted_msg
24
25
26message = 'Hello World!'
27key = 9
28print(f'Caesar Cipher:\nEncrypted: {cc_encrypt(message,key)}\nDecrypted: {cc_decrypt(cc_encrypt(message,key),key)}')
1def encrypt(text,s):
2result = ""
3 # transverse the plain text
4 for i in range(len(text)):
5 char = text[i]
6 # Encrypt uppercase characters in plain text
7
8 if (char.isupper()):
9 result += chr((ord(char) + s-65) % 26 + 65)
10 # Encrypt lowercase characters in plain text
11 else:
12 result += chr((ord(char) + s - 97) % 26 + 97)
13 return result
14#check the above function
15text = "CEASER CIPHER DEMO"
16s = 4
17
18print "Plain Text : " + text
19print "Shift pattern : " + str(s)
20print "Cipher: " + encrypt(text,s)