/
RSA.py
232 lines (203 loc) · 4.21 KB
/
RSA.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
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
import miller_rabin
import mersenne
from fabric.colors import *
import gmpy2
from gmpy2 import *
from datetime import datetime
def seed():
return datetime.now().microsecond
def getRandomPrime():
n = mersenne.retRandom(seed())
if miller_rabin.millerRabin(n, 4):
return n
else:
return getRandomPrime()
def getRandom():
return mersenne.retRandom(seed())
def step2(p, q, r, s):
_n = mul(p,q)
_m = mul(r,s)
_phi = mul((p-1),(q-1))
_lambda = mul((r-1),(s-1))
return (_n, _m, _phi, _lambda)
"""
Derived from Pseudocode: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Iterative_method_2
"""
def extended_gcd(a, b):
lastremainder, remainder = abs(a), abs(b)
x, lastx, y, lasty = 0, 1, 1, 0
while remainder:
lastremainder, (quotient, remainder) = \
remainder, divmod(lastremainder, remainder)
x, lastx = lastx - quotient*x, x
y, lasty = lasty - quotient*y, y
return lastremainder, lastx * \
(-1 if a<0 else 1), lasty * (-1 if b < 0 else 1)
def modinv(a, m):
g, x, y = extended_gcd(a, m)
if g != 1:
raise ValueError
return x%m
"""
Euclid's algorithm to find GCD: http://en.wikipedia.org/wiki/Euclidean_algorithm
"""
def GCD(a, b):
while a!=b:
if a>b:
a -= b
else:
b -= a
return a
def str2NumList(strn):
"""Converts a string to a list of integers based on ASCII values"""
return [ord(chars) for chars in strn]
def numList2String(l):
"""Converts a list of integers to a string bsed on ASCII values"""
return ''.join(map(chr, l))
def generateLargePrime(p):
n = (gmpy2.xmpz(getRandom())**gmpy2.xmpz(p))+(gmpy2.xmpz(getRandom())**gmpy2.xmpz(p)-1)
while not miller_rabin.millerRabin(n, 2):
n = (gmpy2.xmpz(getRandom())**gmpy2.xmpz(p))+(gmpy2.xmpz(getRandom())**gmpy2.xmpz(p)-1)
return n
def encrypt(_g, _s, _e, _n, _m):
r = gmpy2.xmpz(1)
g = gmpy2.xmpz(_g)
s = gmpy2.xmpz(_s)
e = gmpy2.xmpz(_e)
n = gmpy2.xmpz(_n)
m = gmpy2.xmpz(_m)
b1 = f_mod(e, n)
b1 = pow(g, pow(s, b1))
b1 = mul(b1, f_mod(pow(r,m), pow(m,2)))
return b1
def decrypt(_c, _lambda, _m, _d, _mu, _n):
c = gmpy2.xmpz(_c)
lmda = gmpy2.xmpz(_lambda)
m = gmpy2.xmpz(_m)
d = gmpy2.xmpz(_d)
mu = gmpy2.xmpz(_mu)
n = gmpy2.xmpz(_n)
b1 = f_mod(pow((f_mod(mul((((pow(c, lmda) \
% (pow(m, 2))-1))/m), mu), m)),d), n)
return b1
"""http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/"""
def modularExp(a, n, m):
bits = []
while n:
bits.append(n%2)
n /= 2
solution = 1
bits.reverse()
for x in bits:
solution = (solution*solution) % m
if x:
solution = (solution*a) % m
return solution
def expo(u, m):
q = m
prod = 1
current = u
while q>0:
if (q%2)==1:
prod = current * prod
q -= 1
print prod
current = current * current
q = q/2
return prod
# modInv 2
#
def extEuclideanAlg(a, b):
if b==0:
return 1, 0, a
else:
x, y, gcd = extEuclideanAlg(b, a%b)
return y, x-y*(a//b), gcd
def modInvEuclid(a, m):
x, y, gcd = extEuclideanAlg(a, m)
if gcd==1:
return x%m
else:
return None
# Second RSA
p = 0
q = 0
n = 0
phi = 0
e = 0
d = 0
m = 133
c = 0
drc = 0
def selectPrime():
global p
global q
global n
global phi
global e
global d
global m
global c
global drc
p = getRandom()
while not miller_rabin.millerRabin(p, 2):
p = getRandom()
q = getRandom()
while not miller_rabin.millerRabin(q, 2):
q = getRandom()
n = mul(p, q)
phi = (p-1) * (q-1)
e = getRandom()
while gcd(e, phi)!=1:
e=getRandom()
d = divm(1, e, phi)
c = pow(m, e, n)
drc = pow(c, d, n)
# selectPrime()
# print "p: %d"%p
# print "q: %d"%q
# print "n: %d"%n
# print "phi: %d"%phi
# print "e: %d"%e
# print "d: %d"%d
# print "message: %d"%m
# print "cipher: %d"%c
# print "decrypted: %d"%drc
# Karatsuba multiplication #
_CUTOFF = 1536
def k_multiply(x, y):
if x.bit_length()\
<= _CUTOFF \
or \
y.bit_length \
<= \
_CUTOFF:
return x*y
else:
n =\
max(b.bit_length(),
y.bit_length)
half =\
(n+32) // mul(64, 32)
mask =\
(1 << half) - 1
xlow=\
x & mask
ylow =\
y & mask
xhigh =\
x >> half
yhigh =\
y >> half
a = \
k_multiply(xhigh, yhigh)
b =\
k_multiply(xlow + xhigh, \
ylow + yhigh)
c =\
k_multiply(xlow, ylow)
d =\
b - a - c
return (((a << half) + d) \
<< half) \
+ c