-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsacrt.py
More file actions
40 lines (27 loc) · 872 Bytes
/
rsacrt.py
File metadata and controls
40 lines (27 loc) · 872 Bytes
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
from Crypto.Util.number import *
#Chinese Remainder theorem
msg = b'awesomepassword'
p = 10277651387301176987572317325479097395047746036448092597109087343402502311748817759127432792050163773397965509536015380001091183626070519891565314233610391
q = 11477990785403537825829714057028719300154941210754987809122684684976720343684073323701864241970563388461724325124916887772253666524224907489155204051884923
n = p*q
e = 2
phi = (p-1)*(q-1)
msg = bytes_to_long(msg)
print(msg)
while(e<phi): #How e is calculated
if(GCD(e,phi)==1): #GCD is a inbuilt function to give gcd of 2 numbers
break
else:
e=e+1
ciphertext = pow(msg,e,n)
print(ciphertext)
d = inverse(e,phi)
dp = d%(p-1)
dq = d%(q-1)
qinv = inverse(q,p)
#private keys(dp,dq,p,q,qinv)
x1 = pow(ciphertext,dp,p)
x2 = pow(ciphertext,dq,q)
h = (qinv*(x1-x2))%p
plaintext = x2+h*q
print(plaintext)