Created
October 1, 2018 07:13
-
-
Save tunelko/88ad067a0bc4989509a8f61f4f1f475e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import pyasn1.codec.der.encoder | |
import pyasn1.type.univ | |
import base64 | |
# present values on key.pem | |
e = 0x010001 | |
n = 0xC4B07FEFC8E36C05C0DA434221AEBF47DBF70E5189AD892408F3AD81182A9E0700A07C9D7C66C86C7A39602123EE0F605613DE97FDE49FE68396EE0AF3F5F5F3 | |
p = 106173580239682931389627142547722999257831171755485751420548914984291463023277 | |
q = 97024725573258981352721169214486148772531836683230815123426383547032318244639 | |
def egcd(a, b): | |
if a == 0: | |
return (b, 0, 1) | |
else: | |
g, y, x = egcd(b % a, a) | |
return (g, x - (b // a) * y, y) | |
def modinv(a, m): | |
gcd, x, y = egcd(a, m) | |
if gcd != 1: | |
return None | |
else: | |
return x % m | |
# we need to recover this values on private key | |
dp = modinv(e, (p - 1)) | |
dq = modinv(e, (q - 1)) | |
qi = modinv(q, p) | |
phi = (p - 1) * (q - 1) | |
d = modinv(e, phi) | |
def generate_pem(n, e, d, p, q, dP, dQ, qInv): | |
template = '-----BEGIN RSA PRIVATE KEY-----\n{}-----END RSA PRIVATE KEY-----\n' | |
seq = pyasn1.type.univ.Sequence() | |
for x in [0, n, e, d, p, q, dP, dQ, qInv]: | |
seq.setComponentByPosition(len(seq), pyasn1.type.univ.Integer(x)) | |
der = pyasn1.codec.der.encoder.encode(seq) | |
return template.format(base64.encodestring(der).decode('ascii')) | |
key = generate_pem(n, e, d, p, q, dp, dq, qi) | |
key | |
f = open("somevalid.key", "w") | |
f.write(key) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment