- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.py
More file actions
Latest commit
47 lines (39 loc) · 1.27 KB
/
Copy pathRSA.py
File metadata and controls
47 lines (39 loc) · 1.27 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
# coding:utf-8
importrandom
importPrime
# encryption ,according to the formula:m^e = c (mod n) , calculate c ,c == secret,m == message
defencryption(message, puk):
returnPrime.quick_pow_mod(message, puk[1], puk[0])
# decryption ,according to the formula:c^d = m (mod n), calculate m ,
defdecryption(secret, prk):
returnPrime.quick_pow_mod(secret, prk[1], prk[0])
defget_RSAKey():
RSAKey= {}
prime_arr=Prime.get_rand_prime_arr(2)
p=prime_arr[0]
q=prime_arr[1]
whilep==q:
q=random.choice(prime_arr)
n=p*q
s= (p-1) * (q-1)
e=65537
d=Prime.mod_inverse(e, s)
print("p = ", p, ",q = ", q)
print("n = ", n)
print("e = ", e, ",d = ", d)
puk= [n, e]
prk= [n, d]
RSAKey['puk'] =puk
RSAKey['prk'] =prk
returnRSAKey
if__name__=='__main__':
RSAKey=get_RSAKey()
print("Enter a number less and shorter than ", len(str(RSAKey['puk'][0])), ",", RSAKey['puk'][0], ":")
# only encrypt a number type
message=int(input())
secret=encryption(message, RSAKey['puk'])
print("After the encryption data :", secret)
print(len(str(secret)))
message=decryption(secret, RSAKey['prk'])
print("After the decryption data :", message)
print(len(str(message)))