-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCesar Cipher.py
53 lines (51 loc) · 1.54 KB
/
Cesar 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
import os
class Cesar:
dic={'0':'A','1':'B','2':'C','3':'D','4':'E','5':'F','6':'G','7':'H','8':'I',\
'9':'J','10':'K','11':'L','12':'M','13':'N','14':'O','15':'P','16':'Q',\
'17':'R','18':'S','19':'T','20':'U','21':'V','22':'W','23':'X','24':'Y','25':'Z'}
def getKey(self,value):
for k,v in self.dic.items():
if v==value: return int(k)
def encrypt(self,message,key):
message=message.upper()
result=''
for c in message:
if c.isalpha():
result+=self.dic[str((self.getKey(c)+key)%25)]
elif c==' ':
result+=c
return result
def decrypt(self,message,key):
message=message.upper()
result=''
for c in message:
if c.isalpha():
result+=self.dic[str((self.getKey(c)-key)%25)]
else:
result+=c
return result
def main():
c=Cesar()
print("Welcome to Cesar Cipher text encrypter and decrypt.\
\nuse following options for your operation")
ch=input("1.Encrypt\n2.Decrypt\n")
if ch=='1' or ch=='e' or ch.lower()=='encrypt':
message=input("Enter your message to encrypt :")
while True:
key=input("Enter key ( from 0 to 25 ) :")
if key.isdigit() and 0<=int(key) and int(key)<=25:
key=int(key)
break
print("Encrypted message is :",c.encrypt(message,key))
elif ch=='2' or ch=='d' or ch.lower()=='decrypt':
message=input("Enter your message to decrypt :")
while True:
key=input("Enter key ( from 0 to 25 ) :")
if key.isdigit() and 0<=int(key) and int(key)<=25:
key=int(key)
break
print("Decrypted message is :",c.decrypt(message,key))
else:
print("Wrong choise!")
main()
os.system("pause")