How to Create a Self Signed Certificate using Java Keytool

Java Keytool LogoSecuring your Java application with an SSL certificate can be extremely important. Fortunately, it is (usually) quite simple to do using Java Keytool. Most situations require that you buy a trusted certificate, but there are many cases when you can generate and use a self signed certificate for free.

When to Use a Keytool Self Signed Certificate

An SSL certificate serves two essential purposes: distributing the public key and verifying the identity of the server so users know they aren't sending their information to the wrong server. It can only properly verify the identity of the server when it is signed by a trusted third party. A self signed certificate is a certificate that is signed by itself rather than a trusted authority. Since any attacker can create a self signed certificate and launch a man-in-the-middle attack, a user can't know whether they are sending their encrypted information to the server or an attacker. Because of this, you will almost never want to use a self signed certificate on a public Java server that requires anonymous visitors to connect to your site. However, self signed certificates have their place:

Never use a self signed certificate on an e-commerce site or any site that transfers valuable personal information like credit cards, social security numbers, etc.

  • An Intranet. When clients only have to go through a local Intranet to get to the server, there is virtually no chance of a man-in-the-middle attack.
  • A Java development server. There is no need to spend extra cash buying a trusted certificate when you are just developing or testing an application.
  • Personal sites with few visitors. If you have a small personal site that transfers non-critical information, there is very little incentive for someone to attack the connection.

Just keep in mind that visitors will see a warning in their browsers (like the one below) when connecting to a server that uses a self signed certificate until it is permanently stored in their certificate store.

Apache Self signed Certificate Error in Firefox

Generate a Self Signed Certificate using Java Keytool

Now that you know when to use a Keytool self signed certificate, let's create one using a simple Java Keytool command:

  1. Open the command console on whatever operating system you are using and navigate to the directory where keytool.exe is located (usually where the JRE is located, e.g. c:\Program Files\Java\jre6\bin on Windows machines).
  2. Run the following command (where validity is the number of days before the certificate will expire):
    keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
  3. Fill in the prompts for your organization information. When it asks for your first and last name, enter the domain name of the server that users will be entering to connect to your application (e.g. www.google.com)

Java Keytool Self Signed Certificate command

This will create a keystore.jks file containing a private key and your sparklingly fresh self signed certificate. Now you just need to configure your Java application to use the .jks file. If you are using Tomcat, you can follow our Tomcat SSL Installation Instructions.

For more information on creating a Java Keytool Self Signed Certificate, see the following links:

 Digg  del.icio.us  Reddit

Posted on October 30, 2010
Showing comments 1 to 20 of 23 | Next | Last
kalpana
Posts: 21
Comment
How to disable the SSL warning? example:The site's security certificate is not trusted
Reply #23 on : Mon March 03, 2014, 04:14:34
I am working in an application where https page is shown in an iframe.So i want to skip the SSL browser warning page? Can some one suggest how to do that?
Mal
Posts: 21
Comment
Jarsigner
Reply #22 on : Wed January 29, 2014, 10:08:15
I have created a selfsigned certificate using your command and can see in the keystore -list a selfsigned certicate. but when i use jarsigner to apply to my .Jar file it gives me error: Certificate chain not found for keystore.jks. keystore.jks must reference a valid keystore key entry containing a private key and corresponding pulbic key certificate chain.
jojo
Posts: 21
Comment
Re: How to Create a Self Signed Certificate using Java Keytool
Reply #21 on : Wed January 15, 2014, 10:28:07
keytool error
Reply #2 on : Tue March 06, 2012, 23:16:06
Hi,

I followed your step exactly the way you typed but I get an keytool error when I tried to create a self-signed cert.

The error shows: "keytool error: java.io.FileNotFoundException: keystore.jks <Access is denied>
Puneet
Posts: 21
Comment
keytool error: java.io.FileNotFoundException: keystore.jks (Access is denied)
Reply #20 on : Thu October 10, 2013, 02:07:31
Hi,

i have tried generating keys but getting the below exception.
"keytool error: java.io.FileNotFoundException: keystore.jks (Access is denied)"

Do i need the admin rights for generating the keys.

TIA
Puneet Pahuja
Suma Gopalakrishna
Posts: 21
Comment
How to use the key generated using the keytool command
Reply #19 on : Fri July 19, 2013, 07:48:04
If you are using a build.xml and running ant, you would have mentioned a property tag with a file attribute = build.properties or any other filename where you store some variables to be read by ant. Now in the build.properties add the following lines,
key.store=D:\\suma\\MYAPP\\VER1.0\\keystore.jks
key.alias=selfsigned

where key.store should just give absolute path name of the .jks file generated and alias should be assigned whatever you mentioned for -alias in the keytool command.
keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
zac
Posts: 21
Comment
How i can configure Java application to use the .jks file
Reply #18 on : Mon June 03, 2013, 02:14:06
Hi,

The the .jks file was created. What's now?
Youu have got this question already, but i did not founf teh answer for it.

Thanks
Marco
Posts: 21
Comment
How i can configure Java application to use the .jks file
Reply #17 on : Wed May 08, 2013, 06:09:56
I have done all your steps. I have created the .jks file but now i don't now what to do.
Can you explain please?
SANTHOSHKUMAR S
Posts: 21
Comment
need java code for generate keys
Reply #16 on : Thu April 18, 2013, 02:15:48
is there any code available for generate keys via key tool by java code... If it is available, please share it..
Dellanio Alencar
Posts: 21
Comment
It's possible to sign a Java Applet with selfsign certificate?
Reply #15 on : Fri March 15, 2013, 12:21:46
Anyone can tell me if it's possible to sign a Java Applet with selfsign certificate?
pavol
Posts: 21
Comment
Private/Public key pair is where?
Reply #14 on : Mon February 11, 2013, 07:06:12
during generating the self-signed root certificate, key pair is generated. but not in separate fiels but somewhere part of certificate. Is is possible to export both keys?

thanks Pavol
Kumar
Posts: 21
Comment
.keystore file not generated in case of selfsigned
Reply #13 on : Thu December 13, 2012, 16:53:44
Enter key password for <selfsigned>
(RETURN if same as keystore password):
keytool error: java.io.FileNotFoundException: D\tmp\keystore.jks (The system can
not find the path specified)
Joe
Posts: 21
Comment
Encrypted Connection
Reply #12 on : Tue July 24, 2012, 21:43:12
Good stuff, thanks. Can you explain why neither my Firefox nor Explorer browsers are encrypting the info (as reported by FF's Page Info)? I created the cert and changed the Tomcat server.xml and that seems to be working fine.
I was under the impression that a self-signed cert would allow me to send data encrypted, granted from an untrusted site, but if I "accept the risks", shouldn't the data be encrypted?
Shelly
Posts: 21
Comment
SSL email links
Reply #11 on : Tue June 05, 2012, 14:15:22
I need to generate the secured links that can be emailed to the customer. When user clicks on the email link it should perform a Single Sign-On. Could you advise how I can send encrypted URL to customer via email using SSL? I need it urgently. Please help!
Robert
Posts: 2
Comment
Re: keystore.jks where is .crt
Reply #10 on : Sun May 13, 2012, 08:46:09
No .crt file is created. The certificate and the key are both contained in the .jks file.
Shawn
Posts: 21
Comment
where is the keystore saved
Reply #9 on : Fri May 11, 2012, 05:59:31
OK so ive tried this over and over and my key is nowhere to be found. I used jre7\bin to store it and after I created it, its not showing up in that directory? Nor can It be found when I search windows.
rohit
Posts: 21
Comment
keystore.jks where is .crt
Reply #8 on : Fri May 11, 2012, 01:43:57
keystore.jks is generated but where is a certificate?
X.509 certificate is needed
Robert
Posts: 2
Comment
Re: where is the keystore saved?
Reply #7 on : Wed May 09, 2012, 21:12:08
Hi Shawn,

The file is called whatever you specify in the command (it is keystore.jks in the example) and it is placed in the directory where you ran the command.
Shawn
Posts: 21
Comment
where is the keystore saved?
Reply #6 on : Wed May 09, 2012, 11:24:15
After creating a Keystore with the cmd option listed. What is is called and where was it created?
yiannis
Posts: 21
Comment
Client-Server in Android
Reply #5 on : Thu May 03, 2012, 07:41:02
Hi,

i'm writing a client-server application for Android devices, so i don't want to use any browsers. Will the above procedure work?

Thanks.
adytzs
Posts: 21
Comment
Re: How to Create a Self Signed Certificate using Java Keytool
Reply #4 on : Sun April 15, 2012, 05:30:07
Open the cmd.exe by Run as administrator... and it`s work...
Showing comments 1 to 20 of 23 | Next | Last

Write a comment


If you have trouble reading the code, click on the code itself to generate a new random code.
Security Code:
 
Post Comment