Instructions for using the Java Security:keytool tool

Source: Internet
Author: User

Keytool Usage Notes

Keytool is a key and Cert management tool. Use Keytool to manage public key, private key, and certificate associated with key.

1. Command and Option Description 1.1 command

When using the Keytool tool, you can use 15 commands:

1.2 option

option is the parameter of the command, and to understand the parameters of a command can be obtained using KEYTOOL–COMMAND_NAME–HELP. For example: Use Keytool–genkeypair–help to view parameter descriptions for the Genkeypair command:

In this command, there are many commands that use parameters that have default values, which can be unspecified when using commands, and look at the following table:

Option

Default value

-alias

"MyKey"

-keyalg

"DSA" When using the-genkeypair command

"DES" When using the-genseckey command

-keysize

2048 when you use the –genkeypair command, and the-KEYALG option is "RSA"

1024 when you use the –genkeypair command, and the-KEYALG option is "DSA"

256 when the –genkeypair command is used, and the-KEYALG option is "EC"

56 When you use the –genseckey command, and the-KEYALG option is "DES"

168 When you use the –genseckey command, and the-KEYALG option is "Desede"

-validity

90

-keystore

The default filename is. KeyStore, which is a hidden file.

-storetype

The default value is configured in Java.security, and Sun specifies JKS, which can be artificially modified

-file

If it is write, the value is stout, and if it is read, the value is stdin

-protected

False

-sigalg

SHA1WITHDSA If you generate KeyPair using the DSA algorithm

Sha256withrsa If you generate KeyPair using the RSA algorithm

SHA256WITHECDSA If you generate KeyPair using the EC algorithm

2, Commands

2 . 1 generate KeyPair and view

Genkeypair
-genkeypair{-alias alias} {-keyalg Keyalg} {-keysize keysize} {-sigalg sigalg} [-dname dname] [-keypass Keypass] {-STARTDA Te value} {-ext ext}* {-validity valdays} {-storetype Storetype} {-keystore KeyStore} [-storepass Storepass] {-PROVIDERCLA SS Provider_class_name {-providerarg Provider_arg}} {-V} {-protected} {-jjavaoption}

Generates KeyPair, and generates a self-signed certificate using the V3.

Look at the following example:

To view command details, you can use the List command (see the List command below), or you can use a Java program to view it.

Here is an example of a Java program view:

 Packagecom.fjn.other.security;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.security.Key;ImportJava.security.KeyStore;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;Importjava.security.cert.Certificate;ImportJava.security.interfaces.DSAPrivateKey;Importjava.util.Enumeration;Importorg.junit.Test; Public classKeypairereader { Public voidRead (string file, string password)throwsException {fileinputstream fis=NULL; FIS=NewFileInputStream (Newfile); KeyStore KeyStore= Keystore.getinstance ("JKS");        Keystore.load (FIS, Password.tochararray ()); System.out.println (Keystore.containsalias ("MyKey")); Enumeration<String> aliases=keystore.aliases ();  while(Aliases.hasmoreelements ()) {String alias=aliases.nextelement (); String CreateDate=Keystore.getcreationdate (alias). toLocaleString (); System.out.println ("CreateDate:" +createdate); Key Key=Keystore.getkey (alias, Password.tochararray ()); if(Key! =NULL&& KeyinstanceofPrivatekey) {                if("DSA". Equals (Key.getalgorithm (). toUpperCase ())) {Dsaprivatekey Pik=(Dsaprivatekey) key;                System.out.println (Pik.getx ()); }} certificate[] Certs=Keystore.getcertificatechain (alias);  for(Certificate cert:certs) {publickey PUK=Cert.getpublickey ();            System.out.println (PUK); } }} @Test Public voidTest ()throwsException {Read ("D:\\keytooltest\\ca1.jks", "111111"); }}
View Code

The results of the above program execution:

List
-list{-alias alias} {-storetype Storetype} {-keystore KeyStore} [-storepass Storepass] {-providername provider_name} {- Providerclass provider_class_name {-providerarg Provider_arg}} {-V |-rfc} {-protected} {-jjavaoption}

Prints out the information for the specified alias and displays all if alias is not specified.

In the following example, we have typed the key details of alias MyKey generated above using Genkeypair:

2 . 2 certificate signing processing

This process involves 3 of commands:

CertReq, Gencert, Importcert

1) Agency a uses CertReq to generate a certificate signing request file (CSR certificate sign requests), and the resulting file represents a request.

2) after the CA (certificate authority) receives this request, it is processed (using Gencert) and a certificate or certificate chain is generated.

3) Agency a receives the response and imports the certificate (Importcert) into KeyStore.

Before you describe this process, use Genkeypair to generate 2 KeyStore files:

Keytool-genkeypair-alias Ca1-keystore D:\keytooltest\ca1.jkskeytool-genkeypair-alias Ca2-keystore D:\keytooltest\ Ca2.jks

CertReq

To put the request in a file:

Keytool-certreq-alias Ca1-keystore D:\keytooltest\ca1.jks-file D:\keytooltest\ca1_to_ca2.req

Gencert
-GENCERT{-RFC} {-infile infile} {-outfile outfile} {-alias alias} {-sigalg Sigalg} {-dname dname} {-startdate StartDate {- Ext ext}* {-validity valdays} [-keypass Keypass] {-keystore KeyStore} [-storepass Storepass] {-storetype Storetype} {-prov Idername provider_name} {-providerclass provider_class_name {-providerarg Provider_arg}} {-V} {-protected} {- Jjavaoption}
Keytool-gencert-alias ca2-infile D:\keytooltest\ca1_to_ca2.req-outfile D:\keytooltest\ca2_to_ca1.cert-keystore d:\ Keytooltest\ca2.jks

Importcert

Official Study Materials:

Http://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html

Http://docs.oracle.com/javase/7/docs/technotes/tools/windows/keytool.html

Http://docs.oracle.com/javase/8/docs/technotes/tools/windows/keytool.html

In-depth understanding:

Http://www.ibm.com/developerworks/cn/java/j-lo-socketkeytool/index.html?ca=drs

Instructions for using the Java Security:keytool tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.