-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cipher.py
42 lines (36 loc) · 1.09 KB
/
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
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) % num_letters
ciphertext += letters[new_index]
else:
ciphertext += ' '
return ciphertext
print()
print('CAESER CIPHER TASK 1')
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: '))
text = input('Enter the text to encrypt: ')
ciphertext = encrypt(text, key)
print(f'CIPHERTEXT: {ciphertext}')
elif user_input == 'd':
print('DECRYPTION MODE SELECTED')
print()
key = int(input('Enter the key: '))
text = input('Enter the text to decrypt: ')
plaintext = decrypt(text, key)
print(f'PLAINTEXT: {plaintext}')