-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_caesar_cipher.py
56 lines (50 loc) · 1.6 KB
/
python_caesar_cipher.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
46
47
48
49
50
51
52
53
54
55
56
letters = 'abcdefghijklmnopqrstuvwxyz'
number_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 >= number_letters:
new_index -= number_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 < 26:
new_index += number_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('ENCRYPTION MODE SELECTED')
print()
key = int(input('Enter the key (1 through 26): '))
text = input('Enter the text to decrypt')
ciphertext = encrypt(text, key)
print(f'CIPHERTEXT: {ciphertext}')
elif user_input == 'd':
print('ENCRYPTION MODE SELECTED')
print()
key = int(input('Enter the key (1 through 26): '))
text = input('Enter the text to decrypt')
plaintext = encrypt(text, key)
print(f'CIPHERTEXT: {plaintext}')