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