Java program for keystore Maintenance

Source: Internet
Author: User

1 Java program to list all entries in the keystore import java. util. *; import java. io. *; import java. security. *; public class ShowAlias {public static void main (String args []) throws Exception {String pass = "080302"; String name = ". keystore "; FileInputStream in = new FileInputStream (name); KeyStore ks = KeyStore. getInstance ("JKS"); ks. load (in, pass. toCharArray (); Enumeratione = ks. aliases (); while (e. hasMoreElements () {System. out. printl N (e. nextElement () ;}}2 Java program modify keystore password import java. io. *; import java. security. *; public class SetStorePass {public static void main (String args []) throws Exception {char [] oldpass = "080302 ". toCharArray (); char [] newpass = "123456 ". toCharArray (); String name = ". keystore "; FileInputStream in = new FileInputStream (name); KeyStore ks = KeyStore. getInstance ("JKS"); ks. load (in, oldpass); in. close (); FileOut PutStream output = new FileOutputStream (name); ks. store (output, newpass); output. close () ;}3 Java program modify the password of the keystore entry and add the entry package test; import java. io. *; import java. security. *; import java. security. cert. certificate; public class SetKeyPass {public static void main (String args []) throws Exception {// read the relevant parameter String name = ". keystore "; String alias =" mykey "; char [] storepass =" 123456 ". toCharArray (); char [] ol Dkeypassed = "080302 ". toCharArray (); char [] newkeypass = "123456 ". toCharArray (); // obtain the keystore. keystore object, and load the key library FileInputStream in = new FileInputStream (name); KeyStore ks = KeyStore. getInstance ("JKS"); ks. load (in, storepass); // obtain the Certificate chain Certificate [] cchain = ks. getCertificateChain (alias); // read the private key PrivateKey pk = (PrivateKey) ks of the entry corresponding to the alias. getKey (alias, oldkeypass); // Add a new entry ks to the keystore. setKeyEntry (alias, pk, ne Wkeypass, cchain); in. close (); // write the content of the KeyStore object to the new file FileOutputStream output = new FileOutputStream ("333"); ks. store (output, storepass); output. close () ;}4 Java program checks the alias and deletes the entry package test; import java. io. *; import java. security. *; public class DeleteAlias {public static void main (String args []) throws Exception {String pass = "123456"; String name = ". keystore "; String alias =" mykey "; FileInputStream In = new FileInputStream (name); KeyStore ks = KeyStore. getInstance ("JKS"); ks. load (in, pass. toCharArray (); if (ks. containsAlias (alias) {ks. deleteEntry (alias); FileOutputStream output = new FileOutputStream (name); ks. store (output, pass. toCharArray (); System. out. println ("Alias" + alias + "deleted");} else {System. out. println ("Alias not exist") ;}} 5 use a Java program to import signed digital certificates to the keystore. First, read the CA certificate mytest. cer and the signed certificate l received by the user F_signed.cer (issued by the CA private key), uses these two certificates to form a certificate chain, reads the private key from the user's KeyStore, and finally executes the setKeyEntry () of the KeyStore object () method To write the private key and certificate together into the keystore, and use the store () method to save as a file. Import java. io. *; import java. security. *; import java. security. cert. *;/** CA certificate, signed user digital certificate, User Key library name and password, and the private key name of the corresponding certificate, newly generated Certificate Name and keystore name and password */public class ImportCert {public static void main (String args []) throws Exception {// parameter String cacert = "new. cer "; String lfcert =" hqy. cer "; String lfstore =" mykeystore "; char [] lfstorepass =" 080302 ". toCharArray (); char [] lfkeypass = "080302 ". toCharArray (); // CA certificate CertificateFactory cf = CertificateFactory. getInstance ("X.509"); FileInputStream in1 = new FileInputStream (cacert); java. security. cert. certificate cac = cf. generateCertificate (in1); in1.close (); // the user's signature certificate FileInputStream in2 = new FileInputStream (lfcert); java. security. cert. certificate lfc = cf. generateCertificate (in2); in2.close (); // certificate chain java. security. cert. certificate [] cchain = {lfc, cac}; // your KeyStore FileInputStream in3 = new FileInputStream (lfstore); KeyStore ks = KeyStore. getInstance ("JKS"); ks. load (in3, lfstorepass); PrivateKey prk = (PrivateKey) ks. getKey ("new", lfkeypass); // import the certificate ks. setKeyEntry ("new_signed", prk, lfstorepass, cchain); // Save the key library FileOutputStream out4 = new FileOutputStream ("nostore"); ks. store (out4, "080302 ". toCharArray (); out4.close ();}}

Related Article

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.