-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_nofilehandling.py
188 lines (119 loc) · 3.15 KB
/
gui_nofilehandling.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
from Tkinter import *
import random
import math
letters={chr(i):i for i in range(256)}
numbers={}
for letter,num in letters.items():
numbers[num]=letter
def exponentMod(A,B,C) :
if (A == 0):
return 0
if (B == 0):
return 1
if (B % 2 == 0):
y = exponentMod(A, B / 2, C)
y = (y * y) % C
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(x,y):
if(y==0):
return x
else:
return gcd(y,x%y)
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)
d=((k*phi)+1)/e
return (n,d)
'''
f=open("toBeEncrypted.txt","r")
z=f.read()
f.close()
'''
primes=[i for i in range(26,1000) if isPrime(i)]
p=random.choice(primes)
primes.remove(p)
q=random.choice(primes)
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
def change(c):
d=""
while(c>0):
digit=c%10
e=random.randint(0,9)
d+=str(e)+str(digit) #adding random then actual digit , on reversing we get actual digit followed by random no
c=c/10
return d[::-1]
def Encrypt():
tog=""
for i in entryvalue.get():
c=exponentMod(letters[i],public_key[1],n)
s=change(c)
s=s+" "
tog=tog+s
listbox.insert(0,tog)
def changeback(i):
print i
original=""
for n in range(0,len(i)):
if(n%2==0):
original+=i[n]
return int(original)
def Decrypt():
list_of=entryvalue2.get().split()
decrypted_pwd=""
for i in list_of:
cipher=changeback(i)
decryp=exponentMod(cipher,private_key[1],n)
dmsg=numbers[decryp]
decrypted_pwd=decrypted_pwd+dmsg
listbox2.insert(0,decrypted_pwd)
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 = 10, width = 300)
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 = 10, width = 300)
listbox2.pack()
# ******************ciphertext input ending*****************
root.mainloop()