1 Java programs list all entries for KeyStore
Import java.util.*;import java.io.*;import java.security.*;p ublic 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.println (e.nextelement ()); } }}
2 Java program modifies keystore password
Import Java.io.*;import java.security.*;p ublic class setstorepass{public static void Main (String args[]) throws Excep tion{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 (); FileOutputStream output=new FileOutputStream (name); Ks.store (Output,newpass); Output.close (); }}
3 java program modifies the password of the KeyStore entry and adds 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 related parameters String name= ". KeyStore"; String alias= "MyKey"; Char[] storepass= "123456". ToCharArray (); Char[] oldkeypass= "080302". ToCharArray (); Char[] newkeypass= "123456". ToCharArray (); Gets the KeyStore. KeyStore KeyStore Object and loads the KeyStore fileinputstream in=new fileinputstream (name); KeyStore ks=keystore.getinstance ("JKS"); Ks.load (In,storepass); Get the certificate chain for the entry that corresponds to the alias certificate[] Cchain=ks.getcertificatechain (alias); Read the private key of the entry corresponding to the alias Privatekey pk= (Privatekey) Ks.getkey (Alias,oldkeypass); Add a new entry to the KeyStore ks.setkeyentry (Alias,pk,newkeypass,cchain); In.close (); Writes the contents of the KeyStore object to a new file FileOutputStream output=new fileoutputstream ("333"); Ks.store (Output,storepass); Output.close (); }}
4 Java program Check aliases and delete entries
Package Test;import Java.io.*;import java.security.*;p ublic class deletealias{ public static void Main (String args[]) throws exception{ string pass = "123456"; & nbsp string name = ". KeyStore"; string alias = "MyKey"; & nbsp 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 Java programs read certificates directly from KeyStore
Import Java.io.*;import Java.security.*;import Java.security.cert.certificate;public class PrintCert2{public static void Main (string args[]) throws exception{ String pass= "080302"; String alias= "MyKey"; String name= ". KeyStore"; FileInputStream in=new FileInputStream (name); KeyStore ks=keystore.getinstance ("JKS"); Ks.load (In,pass.tochararray ()); Certificate C=ks.getcertificate (alias); In.close (); System.out.println (C.tostring ());} }
Java program implements the management of KeyStore