How to generate rsa keys using specific input numbers in openssl?

I chose 2 primes p and q. Calculated public pair: (n, e) and private key: d. For instance,
p =17, q = 11
n = 187,
e= 7 & d = 23

After sufring on the Internet, I found this command to create a public private key pair:
openssl genrsa -out mykey.pem 1024

But I want to generate a private key corresponding to d = 23, and a public key corresponding to e = 7. How can I give these numbers as input.

+6
source share
2 answers

One way to do this is to create a DER encoded key using the OpenSSL asn1parse -genconf .

You will need to create an input file for asn1parse -genconf to create the RSA key in the standard format (behind RFC 3447 ). The syntax of asn1parse -genconf given here: http://www.openssl.org/docs/crypto/ASN1_generate_nconf.html , and indeed, it already has an example for creating an RSA key.

You need to calculate some more values ​​(in particular, d mod (p-1) , d mod (q-1) and q^-1 mod p . For the values p , q , d you specified:

d mod(p-1) = 23 mod 16 = 7

d mod(q-1) = 23 mod 10 = 3

q^-1 mod p = 14

Put this all together in a text file in the appropriate format:

  asn1 = SEQUENCE: rsa_key

 [rsa_key]
 version = INTEGER: 0
 modulus = INTEGER: 187
 pubExp = INTEGER: 7
 privExp = INTEGER: 23
 p = INTEGER: 17
 q = INTEGER: 11
 e1 = INTEGER: 7
 e2 = INTEGER: 3
 coeff = INTEGER: 14 

To create the DER binary:

  openssl asn1parse -genconf -out newkey.der 

You can then run this through the OpenSSL rsa command to confirm:

  openssl rsa -in newkey.der -inform der -text -check 

What should be output:

  Private-Key: (8 bit)
 modulus: 187 (0xbb)
 publicExponent: 7 (0x7)
 privateExponent: 23 (0x17)
 prime1: 17 (0x11)
 prime2: 11 (0xb)
 exponent1: 7 (0x7)
 exponent2: 3 (0x3)
 coefficient: 14 (0xe)
 RSA key ok
 writing RSA key
 ----- BEGIN RSA PRIVATE KEY -----
 MBwCAQACAgC7AgEHAgEXAgERAgELAgEHAgEDAgEO
 ----- END RSA PRIVATE KEY ----- 

You can use this to encrypt data using the OpenSSL rsautl (although with this key you are limited to encrypting only one byte of data, ensuring that the byte is also less than 187).

+16
source

if you are looking for a method that does not require you to do other calculations than I assume, there are none.

look at rsa_builtin_keygen in the rsa_gen.c source of openssl, it generates prime numbers for itself.

I would suggest you look at the generateKey function here (you will also need a cryptomath file to work)

+1
source

Source: https://habr.com/ru/post/957643/


All Articles