简体   繁体   中英

RSA - Can you create a public key from a private key?

I am creating an encryption strategy for a lab project and want to know if there exists the capability to create a public key from just the private key?

Otherwise, can the public key only be created at the same time as the private key from some key generator?

PS A quick google didnt really help.

Private and public key are created together. Also, the standard storage format for a RSA private key includes all the public key fields, because it is useful for optimized implementations and masking (protection against some side-channel attacks). See the RSA standard itself: PKCS#1 .

Edit: question has been edited, it was originally RSA-only. For other asymmetric algorithm, there is no requirement that the public key may be derived from the private key, nor is there any requirement of the contrary. For discrete logarithm-based algorithms (Diffie-Hellman, El-Gamal, DSA, and the elliptic curve variants of all of these), the public key is easily computed from the private key. It is possible to conceive a degenerate RSA in which knowledge of the private key does not allow reconstruction of the public key, but this requires not storing a few key elements which are needed for good performance (in full details, storing the RSA modulus factors allows for a 4x speed enhancement through the Chinese Remainder Theorem, so everybody stores the factors). On a more conceptual basis, the public key is, well, public, so it is assumed that "everybody" knows it; in practical terms, private key storage format almost always include provisions for storing the public key as well, or at least sufficient data to rebuild the public key.

Yes, you can do this (for some, probably not all, pkc schemes). From the ssh-keygen man file:

-y Read private key file and print public key.

Depends on the algorithm. With RSA, you cannot, with EC you can. However, the public key is usually always stored together with the private key (not the other way around, though, of course), so this is not really a problem (if you have the private key, the same file also includes the public key).

Extracting public RSA key from a private key from the command line

Command line comparison to show there is no difference between a public RSA key and an extracted key if you ignore whitespace.

  1. Generate public private key pairing under home directory with no passphrase and no coment.

    ssh-keygen -t rsa -f ~/id_rsa -N '' -C ""

  2. Generate public key into file 'extracted_public_key'

    ssh-keygen -y -f '/home/vagrant/id_rsa' > extracted_public_key

  3. Diff public key with 'extracted_public_key' file ignoring white space.

    diff -b id_rsa.pub extracted_public_key

Ignoring whitespace at the end of id_rsa.pub there is no difference between a public key and an extracted key.

Actually the public key is mostly generated with the private key together.

If you lost your public key but got the private key, you can still recover the public key from the private key.

All you have to do is to extract the public key from the private key like below:

Extracting the public key from the private key:

ssh-keygen -f~/.ssh/test_rsa -y > ~/.ssh/test_rsa.pub

-f option specifies the file of the key to list the fingerprint for -y option will read a private SSH key file and prints an SSH public key to stdout. The public key part is redirected to the file with the same name as the private key but with the.pub file extension.

NOTE: If the key has a password set, the password will be required to generate the public key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM