-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject_4.py
40 lines (29 loc) · 1.12 KB
/
Project_4.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
#Question Statement
"""Creating a Caesar Cipher: an encryption technique.
a key is set which helps shift the alphabets of the text for encryption,
which is turn will be used to decrypt the text message."""
d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ",',',"?",'"',".", "'","!"]
def encrypt(sentence, key):
final=""
for each in sentence:
if each in d:
pos=d.index(each)
res=d[(pos+key)%len(d)]
print(final+res, end="")
def decrypt(sentence,key):
final=""
for each in sentence:
if each in d:
pos=d.index(each)
res=d[(pos-key)%len(d)]
print(final+res,end="")
repeat="y"
while repeat=="y":
ans=input("\nType 'E' for performing encryption and 'D' for performing decryption: \n")
sen=input("Type your message: \n")
k=int(input("Type the shift number: \n"))
if ans.upper()== "E":
encrypt(sen,k)
elif ans.upper() =="D":
decrypt(sen,k)
repeat=input("\nType 'y' if you wanna go again, otherwise type 'n'. ")