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 private key in java

Public Keys, Private Keys, and Certificates. When performing authentication, SSL uses a technique called public-key cryptography. Public-key cryptography is based on the concept of a key pair, which consists of a public key and a private key. Data that has been encrypted with a public key can be decrypted only with the corresponding private key. RSA Decryption In Java. For decryption we will be using private key and we discussed above that the private key is generated in PKCS#8 format.Hence, following is the code to generate the private key from base64 encoded string using PKCS8EncodedKeySpec. When using this curve and generating your public key, -you- have to choose your private key randomly, in a way that it is impossible for anyone to guess it. The generateor G is a specific point on the elliptic curve, defined in the secp256k1 curve. Steps to generate self-signed PKCS#12 SSL certificate and export its keys: myKeystore.p12 = keystore filename. It can with.pfx extension as well. MYPASSWORD = password used for the keystore and the private key as well. CN = commonName, it will be shown as certiciate name in certificates list.

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 ‘[email protected]’ and we want to generate the public key and name it ‘authorized_keys’. Below is the command to do this.

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

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.