Java encryption and decryption and the operation of digital certificates

Source: Internet
Author: User

1 Java program to implement the maintenance of key repository
1.1 Java program lists all key library entries

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.println (e.nextelement ());  
        }  
}}    

1.2 Java program to modify KeyStore password

Import java.io.*;  
Import java.security.*;  
public class setstorepass{public  
    static void Main (String args[]) throws exception{  
        char[] oldpass= "080302". ToC Hararray ();  
        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 ();   
    }  
    

1.3 Java program to modify password and add entry for KeyStore 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 correlation parameter S    
        Tring name= ". KeyStore";    
        String alias= "MyKey";    
        Char[] storepass= "123456". ToCharArray ();   
        Char[] oldkeypass= "080302". ToCharArray ();    
        Char[] newkeypass= "123456". ToCharArray ();   
        Gets the KeyStore object of the KeyStore. KeyStore and loads the KeyStore fileinputstream in=new fileinputstream (name);   
        KeyStore ks=keystore.getinstance ("JKS");    
        Ks.load (In,storepass);   
        Gets the certificate chain certificate[] Cchain=ks.getcertificatechain (alias) for the corresponding entry for the alias;    
        Read the private key of the entry for 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 the new file FileOutputStream output=newFileOutputStream ("333");    
        Ks.store (Output,storepass);    
    Output.close ();  
 }    
}

1.4 Java Program Check alias and delete 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");}}  
    

2 Java programs read certificates and display certificate designation information
2.1 Java program read certificate from certificate file

Import java.io.*;  
Import java.security.cert.*;  
public class printcert{public  
static void Main (String args[]) throws exception{  
        certificatefactory cf= Certificatefactory.getinstance ("X.509");  
        FileInputStream in=new FileInputStream ("My.cer");  
        Certificate c=cf.generatecertificate (in);    
        In.close ();    
        String s=c.tostring ();    
        Display certificate    
        fileoutputstream fout=new fileoutputstream ("Tmp.txt");    
         BufferedWriter out= New BufferedWriter (New OutputStreamWriter (Fout));    
        Out.write (S,0,s.length ());     
        Out.close ();      
}  
    

2.2 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 ());   
    }  
    

2.3 Java Program displays certificate designation information (full name/public key/signature, etc.)

Import java.io.*;  
Import java.security.*;  
Import java.security.cert.*;    
Import java.math.*;  public class showcertinfo{public static void Main (String args[]) throws exception{Certificatefactory    
        Cf=certificatefactory.getinstance ("X.509");    
        FileInputStream in=new FileInputStream ("My.cer");  
        Java.security.cert.Certificate c=cf.generatecertificate (in);  
        In.close ();    
        X509Certificate t= (X509Certificate) C;    
        SYSTEM.OUT.PRINTLN ("Version number" +t.getversion ());    
        SYSTEM.OUT.PRINTLN ("Serial number" +t.getserialnumber (). toString (16));   
        System.out.println ("Full Name" +t.getsubjectdn ());    
        System.out.println ("Issuer full name n" +t.getissuerdn ());  
        System.out.println ("Expiry date" +t.getnotbefore ());  
        System.out.println ("Expiry date" +t.getnotafter ());   
        SYSTEM.OUT.PRINTLN ("Signature Algorithm" +t.getsigalgname ());   
        Byte[] Sig=t.getsignature (); System.out.println ("Signature n" +new BigInteger (SIG). ToString (16));  
        PublicKey Pk=t.getpublickey ();   
        Byte[] pkenc=pk.getencoded ();   
        SYSTEM.OUT.PRINTLN ("Public Key");   
        for (int i=0;i< div= "" > System.out.print (pkenc[i]+ ",");  
 }    
}  
}

Focus on getting video

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.