-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.py
45 lines (40 loc) · 1.26 KB
/
caesar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
letters = 'abcdefghijklmnopqrstuvwxyz'
num_letters = len(letters)
def encrypt_decrypt(text, mode, key):
result = ' '
if mode == 'd':
key= -key
for letter in text:
letter = letter.lower()
if not letter == ' ':
index = letters.find(letter)
if index == -1:
result += letter
else:
new_index = index + key
if new_index >= num_letters:
new_index -= num_letters
elif new_index < 0:
new_index += num_letters
result += letters[new_index]
return result
print()
print('...CAESAR CIPHER PROGRAM...')
print()
print('Do you want to Encrypt or Decrypt?')
user_input = input('e/d: ').lower()
print()
if user_input == 'e':
print('Encryption mode selected')
print()
key = int(input('Enter Key(1 to 26):'))
text = input('Enter Text to Encrypt: ')
ciphertext = encrypt_decrypt(text, user_input, key)
print(f'Ciphertext: {ciphertext}')
elif user_input == 'd':
print('Encryption mode selected')
print()
key = int(input('Enter Key(1 to 26):'))
text = input('Enter Text to Decrypt: ')
plaintext = encrypt_decrypt(text, user_input, key)
print(f'Plaintext: {plaintext}')