-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
94 lines (75 loc) · 1.89 KB
/
RSA.py
File metadata and controls
94 lines (75 loc) · 1.89 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
import math #RSA
import random
def is_perfect_power(N):
for c in range(2,int(math.log2(N))+1):
if (N**(1/c)).is_integer():
return True
return False
def MillerRabinTest(N, t):
if (N == 2):
return True
if N % 2 == 0: # N>2 is even ?
return False
# check if N is perfect power
# if N**(1/c) is an integer, N is a perfect power
if (is_perfect_power(N) == True):
return False
# Find u odd such that N-1 = 2^r * u
u = N - 1
r = 0
while (u % 2 == 0):
u //= 2
r += 1
for j in range(t):
a = random.randint(1,N)
b = pow(a,u,N) # b = a^u mod N
if (b!=1) and (b!=N-1):
for i in range (r): #(r-1)번 거듭제곱
b = pow(b,2,N)
if b == N-1:
#print(j, i, 'a^{2^i*u}')
break # break and repeat with a new "a"
return False # a^{2^i*u}!=-1 (mod N) for all i
return True
def gen_prime(n, t):
t_MillerRabinTest = n
for i in range(t):
N = random.randrange(2**(n-1)+1, 2**n) # 2^{n-1}+1 <= p < 2^n
if MillerRabinTest(N, t_MillerRabinTest):
return N
return False
def generate_keys(n_bit):
pq_bit = n_bit//2
t=3*pq_bit**2
p = gen_prime(pq_bit,t)
q = gen_prime(pq_bit,t)
n=p*q
return p, q, n
def enc(m,pk):
c = pow(m,pk[1],pk[0])
return c
def dec(c,sk):
m = pow(c,sk[1],sk[0])
return m
if __name__=='__main__':
n_bit_length = 200
(p,q,n) = generate_keys(n_bit_length)
print("p = ",p)
print("q= ", q)
print("n= ",n)
phi = (p-1)*(q-1)
pk=(n,65537)
sk=(n,pow(65537,-1,phi))
m = 12
c = enc(m,pk)
print("ChiperText: ",c)
m = dec(c,sk)
print("Plaintext: ", m)
import rsa
(pk,sk) = rsa.newkeys(512)
print("pk= ",pk)
print("sk= ",sk)
m0 = "Test".encode('utf8')
c = rsa.encrypt(m0,pk)
m1 = rsa.decrypt(c,sk)
print(m1)