Use RSA private key to generate public key?

Keywords: OpenSSL ssh openssh less

I really don't understand this:

Basis: http : //www.madboa.com/geek/openssl/#key-rsa , you can generate a public key from the private key.

openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub

My initial idea was that they were generated together. Does the RSA private key contain the sum? Or public key?

#1 building

In this code, we first create the RSA key, which is private, but it also has a pair of public keys, so in order to get your actual public key, we just do this

openssl rsa -in mykey.pem -pubout > mykey.pub

Hope you get more information Check this out

#2 building

People are looking for SSH public key

If you want to extract the public key for OpenSSH, you need to get the public key in different ways

$ ssh-keygen -y -f mykey.pem > mykey.pub

This public key format is compatible with OpenSSH. Attach the public key to the remote: ~ /. SSH / authorized_

From SSH-KEYGEN(1) document SSH-KEYGEN(1)

ssh-keygen -y [-f input_keyfile]

-y this option will read the private OpenSSH format file and print the OpenSSH public key to stdout.

#3 building

Some people think that the public key is not stored in the PEM file. The following DER structure exists in the private key file:

openssl rsa -text -in mykey.pem

RSAPrivateKey ::= SEQUENCE {
  version           Version,
  modulus           INTEGER,  -- n
  publicExponent    INTEGER,  -- e
  privateExponent   INTEGER,  -- d
  prime1            INTEGER,  -- p
  prime2            INTEGER,  -- q
  exponent1         INTEGER,  -- d mod (p-1)
  exponent2         INTEGER,  -- d mod (q-1)
  coefficient       INTEGER,  -- (inverse of q) mod p
  otherPrimeInfos   OtherPrimeInfos OPTIONAL
}

So there is enough data to calculate the public key (modulus and public index), which is openssl rsa -in mykey.pem -pubout

#4 building

Use the following commands:

1. openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout mycert.pem -out mycert.pem

Loading 'screen' into random state - done
Generating a 2048 bit RSA private key
.............+++
..................................................................................................................................................................+++
writing new private key to 'mycert.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.

2. If you check there will be a file created by the name : mycert.pem

3. openssl rsa -in mycert.pem -pubout > mykey.txt
writing RSA key

4. If you check the same file location a new public key : mykey.txt will be created.

#5 building

My answer below is a bit lengthy, but hopefully it provides some of the details that were missing from the previous answer. I'll start with some related statements and finish with the initial questions.

To encrypt some content using RSA algorithm, you need modulus and encryption (public) exponent pairs (n, e). That's your public key. To decrypt some content using RSA algorithm, you need modulus and decryption (private) index pair (n, d). That's your private key.

To encrypt something with RSA public key, you can treat plaintext as a number and raise it to the power of e modulus n:

ciphertext = ( plaintext^e ) mod n

To decrypt something using the RSA private key, you need to treat the ciphertext as a number and raise it to the power of the d modulus n:

plaintext = ( ciphertext^d ) mod n

To generate a private (d, n) key using openssl, you can use the following command:

openssl genrsa -out private.pem 1024

To generate a public (e, n) key from a private key using openssl, you can use the following command:

openssl rsa -in private.pem -out public.pem -pubout

To parse the contents of the private.pem private RSA key generated by the openssl command, run the following command (the output is truncated to the label here):

openssl rsa -in private.pem -text -noout | less

modulus         - n
privateExponent - d
publicExponent  - e
prime1          - p
prime2          - q
exponent1       - d mod (p-1)
exponent2       - d mod (q-1)
coefficient     - (q^-1) mod p

The private key should not contain only (n, d), right? Why are there six additional components? It contains e (public index) so that public RSA keys can be generated / extracted / derived from private.pem private RSA keys. The remaining five components are used to speed up the decryption process. It has been proved that by pre calculating and storing these five values, RSA can be decrypted four times faster. Decryption will work without these five components, but it can be done faster if you have the convenience. Acceleration algorithm based on Chinese remainder theorem .

Yes, the private.pem RSA private key actually contains all eight values; they are not generated immediately when the last command is run. Try running the following command and comparing the output:

# Convert the key from PEM to DER (binary) format
openssl rsa -in private.pem -outform der -out private.der

# Print private.der private key contents as binary stream
xxd -p private.der

# Now compare the output of the above command with output 
# of the earlier openssl command that outputs private key
# components. If you stare at both outputs long enough
# you should be able to confirm that all components are
# indeed lurking somewhere in the binary stream
openssl rsa -in private.pem -text -noout | less

PKCS This structure of private key is composed of PKCS 1 v1.5 recommendation As an alternative (second) representation. PKCS#1 v2.0 The criteria completely exclude the e and d indices in alternative representations. PKCS#1 v2.1 and v2.2 Further changes to the alternative representation are proposed by optionally including more CRT related components.

To view the contents of the public.pem public RSA key, run the following command (the output is truncated to the label here):

openssl rsa -in public.pem -text -pubin -noout

Modulus             - n
Exponent (public)   - e

There is no surprise here. As promised, it's just (n, e) right.

Now I finally answer the initial question: as shown above, the private RSA key generated by openssl contains components of public key and private key, etc. When you generate / extract / derive a public key from a private key, openssl copies two of its components (e, n) into a separate file that becomes your public key.

Posted by callmecheez on Mon, 24 Feb 2020 04:01:57 -0800