"keytool -genkeypair" Command Examples - Generate Key Pair

Q

How to use the "keytool -genkeypair" command? I want to generate a pair of public key and private key for myself.

✍: FYIcenter.com

A

Here is an example of using "keytool -genkeypair" command to generate a pair of public key and private key for yourself:

C:\Users\fyicenter>"\Program Files\java\jre7\bin\keytool" -genkeypair
-dname CN=www.fyicenter.com -storepass FYIcenter -keypass FYIcenter

What this command did for you:

  • Generate a new pair of public key and private key.
  • Create a self-signed certificate for the public key.
  • Store the certificate and the private together as a new entry in the default keystore file.
  • Set the alias name for the new entry as "mykey".
  • Set the default keystore file password to "FYIcenter".
  • Add a second password "FYIcenter" for the private key.
  • Save the default keystore file as ".keystore" in your home folder C:\Users\fyicenter.

If you run the command again, you should get an error, because Keytool can not add another entry with the same alias name "mykey" in the same keystore file.

C:\Users\fyicenter>"\Program Files\java\jre7\bin\keytool" -genkeypair
-dname CN=www.fyicenter.com -storepass FYIcenter -keypass FYIcenter

keytool error: java.lang.Exception: Key pair not generated, alias <mykey>
already exists

To create another key pair in the same keystore file, use the "-alias" to provide a new name:

C:\Users\fyicenter>"\Program Files\java\jre7\bin\keytool" -genkeypair
-dname CN=jar.fyicenter.com -storepass FYIcenter -keypass FYIcenter
-alias 2ndkey

2012-07-19, 12151👍, 0💬