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
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
1x = input()
2NUM_LETTERS = 26
3def SpyCoder(S, N):
4 y = ""
5 for i in S:
6 if(i.isupper()):
7 x = ord(i)
8 x += N
9 if x > ord('Z'):
10 x -= NUM_LETTERS
11 elif x < ord('A'):
12 x += NUM_LETTERS
13 y += chr(x)
14 else:
15 y += " "
16 return y
17
18def GoodnessFinder(S):
19 y = 0
20 for i in S:
21 if i.isupper():
22 x = ord(i)
23 x -= ord('A')
24 y += letterGoodness[x]
25 else:
26 y += 1
27 return y
28
29def GoodnessComparer(S):
30 goodnesstocompare = GoodnessFinder(S)
31 goodness = 0
32 v = ''
33 best_v = S
34 for i in range(0, 26):
35 v = SpyCoder(S, i)
36 goodness = GoodnessFinder(v)
37 if goodness > goodnesstocompare:
38 best_v = v
39 goodnesstocompare = goodness
40 return best_v
41
42
43print(GoodnessComparer(x))
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)}')