Java program implements the management of KeyStore

Source: Internet
Author: User

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

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.