-
Notifications
You must be signed in to change notification settings - Fork 4
/
p39.py
64 lines (45 loc) · 1.4 KB
/
p39.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
from math import gcd
from main import Solution
from Crypto.Util.number import getPrime, getRandomInteger
def invmod(n: int, mod: int) -> int:
n = n % mod
t, newt = 0, 1
r, newr = mod, n
while newr != 0:
q = r // newr
tmp1, tmp2 = t, r
t = newt
newt = tmp1 - q * newt
r = newr
newr = tmp2 - q * newr
if r > 1:
return 0
elif t < 0:
return t + mod
else:
return t
class RSA:
def __init__(self, bitsize: int = 1024):
self.e = 3
self.bitsize = bitsize
p, q = getPrime(bitsize // 2), getPrime(bitsize // 2)
while int.bit_length(p * q) != bitsize:
p, q = getPrime(bitsize // 2), getPrime(bitsize // 2)
phi, self.N = (p - 1) * (q - 1), p * q
while gcd(phi, self.e) != 1:
p, q = getPrime(bitsize // 2), getPrime(bitsize // 2)
phi, self.N = (p - 1) * (q - 1), p * q
self._d = invmod(self.e, phi)
def enc(self, m: int) -> int:
return pow(m, self.e, self.N)
def dec(self, c: int) -> int:
return pow(c, self._d, self.N)
def p39() -> str:
m = getRandomInteger(31)
rsa = RSA(bitsize=32)
print(f'Initialized 32 bit RSA: e = {rsa.e}, N = {rsa.N}')
c = rsa.enc(m)
print(f'Enc({m}) = {c}')
return f'Dec({c}) = {rsa.dec(c)}'
def main() -> Solution:
return Solution('39: Implement RSA', p39)