Can I Generate A Public Key From A Private Key
Lets say you have a private/public key pair that you use to login to your server via SSH and you lose the public key, either it was deleted or corrupt and you don’t want to have to regenerate a new pair what options do you have? In this post I will demonstrate how to regenerate a public key from the corresponding private key that you still have.
Generate public key and store into a file
Depends on the algorithm. Armies of exigo key generator. 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). In a nutshell, you will generate a public and private key pair. The public key will be placed on the server by your system administrator, giving you access. You will keep the file containing the private key in a safe place. You’ll login by simply by providing that private key file to your SSH or SFTP supporting client. The.pub file is your public key, and the other file is the corresponding private key. If you don’t have these files (or you don’t even have a.ssh directory), you can create them by running a program called ssh-keygen, which is provided with the SSH package on Linux/macOS systems and comes with Git for Windows. Nov 01, 2018 A private key can be use to sign a document and the public key is use to verify that the signature of the document is valid. The API we use to generate the key pairs is in the java.security package. That’s mean we have to import this package into our code. The class for generating the key pairs is KeyPairGenerator.
Generate Public Key From Private Key
It is a simple one liner command to generate a public key from a private key, so lets say our private key is named ‘user@myserver.key’ and we want to generate the public key and name it ‘authorized_keys’. Below is the command to do this.
user@workstation:~$ssh-keygen-y-fuser@myserver.key>authorized_keys |
command breakdown:
‘ssh-keygen -y’ : tells the prorgram ssh-keygen to output a public key
‘-f user@myserver.key’: references the key file user@myserver.key to generate the public key from
to generate the missing public key again from the private key, the following command will generate the public key of the private key provided with the -f option. $ ssh-keygen -y -f /.ssh/idrsa /.ssh/idrsa.pub enter passphrase: the -y option will read a private ssh key file and prints an ssh public key to stdout. the public key part is. sep 27, 2018 when we generate our key pair with openssl, we see a 256-bit private key (and made from 32 bytes), along with a 65 bytes of a public key. the 04 at the start of the public key is an identifier.
‘> authorized_keys’: instead of printing the output to the console redirect the output (public key) to a file named ‘authorized_keys’
generate private and public key
copy public key to your server
now we will need to take our authorized_keys file and store it on the server, if we can currently login via ssh to our server we can use rsync over ssh to securely copy the public key to the server, although this key is displayed to anyone who attempts to login to your server it is smart to use ssh or another secure method to copy it up to the server to ensure that the key is not tampered with while it is being transmitted.
below is an example using rsync to copy the authorized keys file to its destination on the server and removing it from your local computer.