It is often required to use digital signature implementation in various projects. In this guide I'm posting simple steps to generate a key pair or import a certificate to a Keystore using keytool.
Make sure that you have JDK installed and path for jdk is set in Environment Variables.
Note : If path is not set then follow these steps from /jdk/bin/ directory.
Create a Private/Public Key Pair with Keytool
1. Run the below command to specify or create the keystore.
keytool -genkey -alias teiid -keyalg RSA -validity 365 -keystore server.keystore -storetype JKS
-alias : Name to identify in keystore
-keylag : Algorithm to be used in key generation
-keystore : The name of the keystore with type .jks
2. If the specified keystore already exists, enter the existing password for that keystore, otherwise enter a new password:
Enter keystore password: <password>
3. Answer the following questions when prompted:
What is your first and last name?
[Unknown]: <user's name>
What is the name of your organizational unit?
[Unknown]: <department name>
What is the name of your organization?
[Unknown]: <company name>
What is the name of your City or Locality?
[Unknown]: <city name>
What is the name of your State or Province?
[Unknown]: <state name>
What is the two-letter country code for this unit?
[Unknown]: <country name>
Note : It is adviced to set first and last name/CN(common name) as domain name or IP address of host machine.
4. Enter yes to confirm the provided information is correct:
Is CN=<user's name>, OU=<department name>, O="<company name>",
L=<city name>, ST=<state name>, C=<country name> correct?
[no]: yes
5. Enter your desired keystore password:
Enter key password for <server>
(Return if same as keystore password)
With above steps server.keystore file contains the newly generated public and private key pair.
Import public key certificate in keystore
Another common requirement is to export and import .cer public key certificate in trusted keystore.
Export:
keytool -v -export -file public.cer -keystore server.jks -alias alias
Import:
keytool -import -alias alias -file public.cer -keystore server.jks