-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
279 lines (223 loc) · 6.36 KB
/
RSA.py
File metadata and controls
279 lines (223 loc) · 6.36 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import random
'''partner's N & e are 775315049 & 72084229 '''
options = {
1: 'Generate Keys',
2: 'Encrypt',
3: 'Decrypt',
4: 'Sign',
5: 'Verify',
6: 'Exit',
}
def menu():
for option in options.keys():
print (option, ':', options[option] )
''' Function : To check prime '''
def isPrime(a):
if a > 1:
for i in range(2, int(a/2)+1):
if (a % i) == 0:
return False
return True
else:
return False
''' Function : To get prime no's '''
def getPrimeNo():
while True:
z = random.randrange(32769, 65532)
if isPrime(z):
return z
''' Function : To calcuate gcd '''
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
''' Function : To calculate Inverse of e :
using Extended Eculidean Method '''
def inverse(a, b) :
b1 = b
y = 0
x = 1
if (b == 1) :
return 0
while (a > 1) :
q = a // b
t = b
b = a % b
a = t
t = y
y = x - q * y
x = t
# Make number positive by adding b1
if (x < 0) :
x = x + b1
return x
''' Key generator Function '''
def generate_keypair(p, q,f,n):
#Choose an random integer from range 2 - f(n)
e = random.randrange(2, f)
# check (e , f(n)) are comprime
g = gcd(e, f)
while g != 1:
e = random.randrange(2, f)
g = gcd(e, f)
#call to Extended Eculidean function
d = inverse(e, f)
#Keypairs
return ((e, n), (d, n))
''' Function : To generate keys '''
def generate_keys():
p = getPrimeNo()
q = getPrimeNo()
n = p*q # n=pq
f = (p-1)*(q-1) # f(n)=(p-1)(q-1)
primes = (p,q)
publicKey , privateKey = generate_keypair(p,q,f,n)
print("(p,q) : ", primes)
print("FiN : ", f)
print("(e,n) : ", publicKey)
print("(d,n) : ", privateKey)
return publicKey, privateKey
hex_list = []
num_list = []
cipher_list = []
''' Function : To encrypt plaintext '''
def encrypt():
e = int(input('value of e: '))
n = int(input('value of n: '))
publicKey = (e, n)
print('Handle option \'Option 2\'')
message = input('Enter message m = ') # Message to be encrypted
print('Public Key [e,n] = ',publicKey)
# convert message into chunks of 3 each
chunks = [message[i:i+3] for i in range(0, len(message), 3)]
print(chunks)
#perform actions on every chunk
for i in chunks:
#encode chunks to Hexadecimal
hexa = i.encode().hex()
#convert Hexadecimal to Interger
num = int(hexa, 16)
hex_list.append(hexa)
num_list.append(num)
#encrypt using m^e mod n
cipher = pow(num, e, n)
cipher_list.append(cipher)
#return Lists
return hex_list, num_list, cipher_list
plain_list = []
''' Function : To decrypt cipher '''
def decrypt():
ciphers = [296903278, 1581324012, 1457336654, 485798237, 1066240178, 561749684, 1264687844]
# d = int(input('value of d: '))
# n = int(input('value of n: '))
# p = int(input('value of p: '))
# q = int(input('value of q: '))
d = 740376845
p = 46523
q = 36383
n = 1692646309
# p=56941
# q=51461
# n=2930240801
# d=2813983841
# ciphers = [1143665784, 2272409881, 1222390195, 2139764264]
for i in ciphers:
# By chinese Reminder Theorem
mp = inverse(q,p)
mq = inverse(p,q)
dp = d % (p-1)
dq = d % (q-1)
xp = i**dp % p
xq = i**dq % q
x = ((mp*q*xp)+(mq*p*xq))%n
# convert number to Hexadecimal
hexa = hex(x)
hexa = hexa.replace("0x","")
byte_objects = bytes.fromhex(hexa)
# Convert to ASCII
plain = byte_objects.decode("ASCII")
plain_list.append(plain)
return plain_list
def square_and_multiply(i, d, n):
r = 1
for bit in list(bin(d)[2:]):
r = (r * r) % n
if int(bit) == 1:
r = (r * i) % n
return r
def signature_sign():
d = 740376845
n = 1692646309
sign_hex_list = []
sign_num_list = []
sign_sign = []
message = "Himanshu" # Message to be encrypted
print(message)
# convert message into chunks of 3 each
chunks = [message[i:i+3] for i in range(0, len(message), 3)]
print(chunks)
#perform actions on every chunk
for i in chunks:
# encode chunks to Hexadecimal
hexa = i.encode().hex()
#convert Hexadecimal to Interger
num = int(hexa, 16)
sign_hex_list.append(hexa)
sign_num_list.append(num)
sign = square_and_multiply(num, d, n)
sign_sign.append(sign)
return sign_sign
def signature_verify():
e = 72084229
n = 775315049
plain_list = []
message = "Himani Rajput" # Message to be encrypted
signature = [649383299, 344090736, 465105025, 282068103, 228150614]
print(message)
print(signature)
#perform actions on every chunk
for i in signature:
x = square_and_multiply(i, e, n)
hexa = hex(x)
hexa = hexa.replace("0x","")
byte_objects = bytes.fromhex(hexa)
# Convert to ASCII
plain = byte_objects.decode("ASCII")
plain_list.append(plain)
print(plain_list)
if message == "".join(plain_list):
print('IS_VALID_SIGNATURE = True')
else:
print('IS_VALID_SIGNATURE = False')
''' Main '''
if __name__=='__main__':
while(True):
menu()
option = ''
try:
option = int(input('Enter your choice: '))
except:
print('Wrong input. Please enter a number ...')
#Check what choice was entered and act accordingly
if option == 1:
generate_keys()
elif option == 2:
encrypt()
print(hex_list)
print(num_list)
print("Encrypted text : ",cipher_list)
elif option == 3:
decrypt()
print(plain_list)
print("Partner's Decrypted Message is : " + "".join(plain_list))
elif option == 4:
out = signature_sign()
print(out)
elif option == 5:
signature_verify()
elif option == 6:
print('Thanks you for computing')
exit()
else:
print('Invalid option. Please enter a number between 1 and 4.')