Generate RSA private key from n, e, d, p, q values

2019-05-10 23:03发布

问题:

This question already has an answer here:

  • How to Generate rsa keys using specific input numbers in openssl? 2 answers

I have calculated n, e, d, p, q values of an RSA key. Now, how can I generate a private key file (pem or der) with openssl command line tools?

I was thinking about

openssl asn1parse -genconf asn1.cnf -noout -out asn1.der

but I cannot understand how to build the conf file.

回答1:

The OpenSSL manpage for asn_generate_nconf comes with an example cnf for generating the private_key ASN.1 sequence that should work with your cmdline:

asn1=SEQUENCE:private_key
[private_key]
version=INTEGER:0

n=INTEGER:0xBB6FE79432CC6EA2D8F970675A5A87BFBE1AFF0BE63E879F2AFFB93644\
D4D2C6D000430DEC66ABF47829E74B8C5108623A1C0EE8BE217B3AD8D36D5EB4FCA1D9

e=INTEGER:0x010001

d=INTEGER:0x6F05EAD2F27FFAEC84BEC360C4B928FD5F3A9865D0FCAAD291E2A52F4A\
F810DC6373278C006A0ABBA27DC8C63BF97F7E666E27C5284D7D3B1FFFE16B7A87B51D

p=INTEGER:0xF3929B9435608F8A22C208D86795271D54EBDFB09DDEF539AB083DA912\
D4BD57

q=INTEGER:0xC50016F89DFF2561347ED1186A46E150E28BF2D0F539A1594BBD7FE467\
46EC4F

exp1=INTEGER:0x9E7D4326C924AFC1DEA40B45650134966D6F9DFA3A7F9D698CD4ABEA\
9C0A39B9

exp2=INTEGER:0xBA84003BB95355AFB7C50DF140C60513D0BA51D637272E355E397779\
E7B2458F

coeff=INTEGER:0x30B9E4F2AFA5AC679F920FC83F1F2DF1BAF1779CF989447FABC2F5\
628657053A

As an alternative rsatool.py can generate the base64 encoded ASN.1 privkey.pem or plain asn.1 der sequence with a simple cmdline

#>python rsatool.py -n <n> -p <p> -q <q> -e <e> -v PEM -o privkey.pem

or for output in ASN.1 der:

#>python rsatool.py -n <n> -p <p> -q <q> -e <e> -v DER -o privkey.key