-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVigenere.py
69 lines (54 loc) · 1.73 KB
/
Vigenere.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
60
61
62
63
64
65
66
67
68
69
import re
print("1.Encryption")
print("2.Decryption")
choice=int(input("Enter A Choice="))
if(choice==1):
file1 = open("plaintext.txt", "r")
file2 = open("cipher.txt", "w")
pt = file1.read().replace(" ", "").upper()
pt = re.sub('[^a-zA-Z]', '', pt)
key = input(("Enter the Key="))
key = key.replace(" ", "").upper()
key = re.sub('[^a-zA-Z]', '', key)
if ((len(pt)) > len(key)):
key = list(key)
for i in range(len(pt) - len(key)):
key.append(key[i % len(key)])
print("\n********Key***********")
print(str(key))
print("**********************\n")
print("Plain Text=" + pt +"\n")
print("*****ENCRYPTION*******")
ct=""
for i in range(len(pt)):
asci=(ord(pt[i])+ord(key[i]))%26
asci+=ord('A')
ct=ct+chr(asci)
print("\nThe ENCRYPTED TEXT IS= "+ct)
print("\n**********************")
file2.write(ct)
file2.close()
if(choice==2):
file3 = open("cipher.txt", "r")
file4=open("plaintext.txt","w")
ctxt = file3.read()
key = input(("Enter the Key="))
key = key.replace(" ", "").upper()
key = re.sub('[^a-zA-Z]', '', key)
if ((len(ctxt)) > len(key)):
key = list(key)
for i in range(len(ctxt) - len(key)):
key.append(key[i % len(key)])
print("\n********Key***********")
print(str(key))
print("**********************\n")
print("Cipher Text= "+ctxt+"\n")
print("*****DECRYPTION*******\n")
ctxt=list(ctxt)
ptxt=""
for i in range(len(ctxt)):
txt=(ord(ctxt[i])-ord(key[i])+26)%26
txt+=ord('A')
ptxt+=chr(txt)
print("The DECRTYPED Text is="+ptxt)
file4.write(ptxt)