-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathP40_CipherText.py
59 lines (50 loc) · 1.72 KB
/
P40_CipherText.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
57
58
59
# Author: OMKAR PATHAK
# This program illustrates a simple example for encrypting/ decrypting your text
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LETTERS = LETTERS.lower()
def encrypt(message, key):
''' This function lets you to encrypt your message based on a key '''
encrypted = ''
for chars in message:
if chars in LETTERS:
num = LETTERS.find(chars)
num += key
if num>25:
num=num%25
num=num-1
encrypted =encrypted + LETTERS[num]
return encrypted
def decrypt(message, key):
''' This function lets you to decrypt your message based on a key '''
decrypted = ''
for chars in message:
if chars in LETTERS:
num = LETTERS.find(chars)
if num>25:
num=num%25
num=num-1
num = num -key
decrypted =decrypted+LETTERS[num]
return decrypted
def main():
message = str(input('Enter your message: '))
key = int(input('Enter you key [1 - 26]: '))
choice = input('Encrypt or Decrypt? [E/D]: ')
if choice.lower().startswith('e'):
print(encrypt(message, key))
else:
print(decrypt(message, key))
if __name__ == '__main__':
main()
# OUTPUT:
# omkarpathak@omkarpathak-Inspiron-3542:~/Documents/GITs/Python-Programs/Programs$ python P40_CipherText.py
# Enter your message: omkar
# Enter you key [1 - 26]: 2
# Encrypt or Decrypt? [E/D]: e
# qomct
#
# omkarpathak@omkarpathak-Inspiron-3542:~/Documents/GITs/Python-Programs/Programs$ python P40_CipherText.py
# Enter your message: qomct
# Enter you key [1 - 26]: 2
# Encrypt or Decrypt? [E/D]: d
# omkar