-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
200 lines (134 loc) · 3.53 KB
/
gui.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from Tkinter import *
import random
import math
letters={'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25}
numbers={}
for letter,num in letters.items():
numbers[num]=letter
def exponentMod(A,B,C) :
# Base cases
if (A == 0):
return 0
if (B == 0):
return 1
# If B is even
if (B % 2 == 0):
y = exponentMod(A, B / 2, C)
y = (y * y) % C
# If B is odd
else:
y = A % C
y = (y * exponentMod(A, B - 1, C) % C) % C
return (int)((y + C) % C)
def isPrime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
def gen_public_key(n,phi):
list_random_e=[i for i in range(1,phi) if (n%i!=0 and gcd(i,phi) ==1)]
e=random.choice(list_random_e)
return (n,e)
def gen_private_key(n,phi,e):
list_random_k=[i for i in range(1,phi) if (((i*phi)+1)%e ==0)]
k=random.choice(list_random_k)
print "k=",k
d=((k*phi)+1)/e
return (n,d)
'''create=[]
z=raw_input("enter what has to be encrypted :")
for i in range(0,len(z)):
if(z[i].isupper()):
create.append(-1)
else:
create.append(0)
z=z.lower()
z=raw_input("enter what has to be encrypted :")
print "z=",z'''
f=open("toBeEncrypted.txt","r")
z=f.read(2)
f.close()
msg=0
i=0
for ch in z[::-1]:
msg+=(letters[ch]*(26**i))
i+=1
print "Original message=\t",msg
primes=[i for i in range(msg/2,msg) if isPrime(i)]
p=random.choice(primes)
primes.remove(p)
q=random.choice(primes)
print "p=",p
print "q=",q
n=p*q
phi=(p-1)*(q-1)
public_key=gen_public_key(n,phi)
print "public key (n,e) : ",public_key
private_key=gen_private_key(n,phi,public_key[1])
print "private key (n,d) ",private_key
#cipher=exponentMod(msg,public_key[1],n)
def Encrypt():
c=exponentMod(msg,public_key[1],n)
print "encrypted=",c
f=open("encrypted.txt","w")
s=str(c)
f.write(s)
f.close()
listbox.insert(0,c)
def Decrypt():
f=open("encrypted.txt","r")
s=f.read()
cipher=int(s)
f.close()
decryp=exponentMod(cipher,private_key[1],n)
print "decrypted=",decryp
dmsg=""
while(decryp>0):
r=decryp%26
dmsg+=numbers[r]
decryp/=26
dmsg=dmsg[::-1]
f=open("Decrypted.txt","w")
f.write(dmsg)
f.close
print "The decrypted message is :",dmsg
listbox2.insert(0,dmsg)
root = Tk()
# GUI title
root.title('RSA ENCRYPTOR AND DECRYPTOR')
# ******************plaintext input*****************
l = Label(root,text='Input the plaintext')
l.pack()
# input plaintext
entryvalue = Entry(root)
entryvalue.pack()
# click the Encrypt button
button = Button(root,text="Encrypt",command=Encrypt)
button.pack()
# show the ciphertext info.
show = Label(root,text='Show Ciphertext:')
show.pack()
listbox = Listbox(root,height = 1, width = 40)
listbox.pack()
# ******************plaintext input ending*****************
# ******************ciphertext input*****************
label = Label(root,text='Input the ciphertext')
label.pack()
# input ciphertext
entryvalue2 = Entry(root)
entryvalue2.pack()
# click the Decrypt button
button2 = Button(root,text="Decrypt",command=Decrypt)
button2.pack()
# show the plaintext info.
show2 = Label(root,text='Show Plaintext:')
show2.pack()
listbox2 = Listbox(root,height = 1, width = 40)
listbox2.pack()
# ******************ciphertext input ending*****************
root.mainloop()