-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (52 loc) · 1.68 KB
/
main.py
File metadata and controls
61 lines (52 loc) · 1.68 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
letters = 'abcdefghijklmnopqrstuvwxyz'
num_letters = len(letters)
def encrypt(plaintext, key):
ciphertext = ''
for letter in plaintext:
letter = letter.lower()
if not letter == '':
index = letters.find(letter)
if index == -1:
ciphertext += letter
else:
new_index = index + key
if new_index >= num_letters:
new_index -= num_letters
ciphertext += letters[new_index]
return ciphertext
def decrypt(ciphertext, key):
plaintext = ''
for letter in ciphertext:
letter = letter.lower()
if not letter == '':
index = letters.find(letter)
if index == -1:
plaintext += letter
else:
new_index = index - key
if new_index < 0:
new_index += num_letters
plaintext += letters[new_index]
return plaintext
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('ENCRYPION MODE SELECTED')
print()
key = int(input('enter your key(1 through 26): '))
text = input('enter the text to encrypt: ')
ciphertext = encrypt(text, key)
print(f'ciphertext: {ciphertext}')
elif user_input == 'd':
print('DECRYPION MODE SELECTED')
print()
key = int(input('enter your key(1 through 26): '))
text = input('enter the text to decrypt: ')
plaintext = decrypt(text, key)
print(f'plaintext: {plaintext}')
else:
print('invalid input')