Java call Keytool generate KeyStore and CER certificates

Source: Internet
Author: User
Tags getmessage asymmetric encryption

Keytool is a management tool for Java data certificates.

Keytool the key (key) and certificate (certificates) exist in a file called KeyStore in KeyStore,

Contains two types of data:
Key Entity--key (secret key) or private key and paired public key (with asymmetric encryption)
Trusted certificate Entity (trusted certificate entries)--only public key is included

Ailas (alias) Each keystore is associated with this unique alias, which is usually case-insensitive

The following is an example test of Java calling Keytool generating KeyStore and CER certificates :

 Public classExportcertformkeystore { Public voidexeccommand (string[] arstringcommand) { for(inti = 0; i < arstringcommand.length; i++) {System.out.print (Arstringcommand[i]+ " "); }        Try{runtime.getruntime (). exec (Arstringcommand); } Catch(Exception e) {System.out.println (E.getmessage ()); }    }     Public voidexeccommand (String arstringcommand) {Try{runtime.getruntime (). exec (Arstringcommand); } Catch(Exception e) {System.out.println (E.getmessage ()); }    }    /*** Generate key*/     Public voidGenkey () {string[] Arstringcommand=Newstring[] {"cmd", "/k",                "Start",//cmd shell command"Keytool",                "-genkey",//-genkey indicates that the key is generated"-validity",//-validity Specify the certificate validity period (in days), this is 36,000 days"36500",                "-keysize",//Specify the key length"1024",                "-alias",//-alias Specifies the alias, here is the SS"SS",                "-keyalg",//-keyalg The algorithm that specifies the key, such as RSA DSA (if you do not specify DSA by default )"RSA",                "-keystore",//-keystore Specify the storage location, this is D:/demo.keystore"D:/demo.keystore",                "-dname",//cn= (First and last name), ou= (organizational unit name), o= (organization name), L= (city or region name) ,//st= (state or province name), c= (two-letter country code for the unit) ""Cn= (ss), ou= (ss), o= (ss), l= (BJ), st= (BJ), c= (CN)",                "-storepass",//Specify the password for the KeyStore (the password required to obtain the KeyStore information)"123456",                 "-keypass",//Specify the password for the alias entry (the password for the private key)"123456",                 "-V"//-V Show certificate details in keystore        };    ExecCommand (Arstringcommand); }    /*** Export certificate file*/     Public voidExport () {string[] Arstringcommand=Newstring[] {"cmd", "/k",                "Start",//cmd shell command"Keytool",                "-export",//-Export is specified as exporting Operation"-keystore",//-keystore Specify KeyStore file, here is D:/demo.keystore"D:/demo.keystore",                "-alias",//-alias Specifies the alias, here is the SS"SS",                "-file",//-file pointing to the export path"D:/demo.cer",                "-storepass",//Specify the password for the KeyStore"123456"                        };        ExecCommand (Arstringcommand); }}

JUnit Test Cases:

 import   Org.junit.Test;  public  class   exportcertformkeystoretest {@Test  public
     void   Genkeytest () { //  generate key test  new   Exportcertformkeystore (). Genkey (); } @Test  public  void   Exporttest () { //  export certificate file Test  new      Exportcertformkeystore (). Export (); }}

After running the test case, two files are generated below the root of the D drive:

Demo.keystore

Demo.cer

Java call Keytool generate KeyStore and CER certificates

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.