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
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)