Generate Public Key From Private

Posted on by
Generate Public Key From Private Rating: 3,5/5 9968 votes

Press generate and follow instructions to generate (public/private) key pair. Create a new 'authorizedkeys' file (with Notepad): Copy your public key data from the 'Public key for pasting into OpenSSH authorizedkeys file' section of the PuTTY Key Generator, and paste the key data to the 'authorizedkeys' file. Save the text file in the same folder where you saved the private key, using the.pub extension to indicate that the file contains a public key. If you or others are going to use an SSH client that requires the OpenSSH format for private keys (such as the ssh utility on Linux), export the private key: On the Conversions menu, choose Export.

Generate Public Key From Private

The code snippet below show you how to use the JDK Security API to generate public and private keys. 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. To get an instance of this class we have to call the getInstance() methods by providing two parameters. The first parameter is algorithm and the second parameter is the provider.

Generate public key from private key java

After obtaining an instance of the key generator we have to initialize it. The initialize() method takes two parameters, the key size and a source of randomness. We set the key size to 1024 and pass and instance of SecureRandom.

Finally to generate the key pairs we call the generateKeyPair() method of the KeyPairGenerator class. This will return a KeyPair object from where we can get the PrivateKey and PublicKey by calling the getPrivate() and getPublic() method.

Let’s see the code snippet below:

  • How do I backup MySQL databases in Ubuntu? - December 16, 2019
  • How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
  • How to Install Consolas Font in Mac OS X? - March 29, 2019

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

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.

2
4
6
user@workstation:~$rsync-avz--remove-source-files authorized_keys user@remote_server_ip_address:/home/user/.ssh/authorized_keys
test
sent95bytes received43bytes92.00bytes/sec
user@workstation:~$

At this point our public key file is on our server where it is supposed to be in our users ‘.ssh’ directory.

Set proper permissions for public key

Lastly we will need to make sure our permissions are set properly on our public key to prevent anyone else tampering with it. We should give our key file RW, R, R permissions, below is the command to properly set permission on the key.