The Java program implements the operation of the certificate

Source: Internet
Author: User

1 Javaprogram reads certificate from certificate file
Import Java.io.*;import java.security.cert.*;p ublic class printcert{public static void Main (String args[]) throws Except ion{        certificatefactory cf=certificatefactory.getinstance ("the");        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 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 ());}    }  


3 Java program displays certificate specified information (full name/public key/signature, etc.)
Import java.io.*;import java.security.*;import java.security.cert.*;import java.math.*;p ublic class ShowCertInfo{ public static void Main (String args[]) throws exception{certificatefactory cf=certificatefactory.getinstance ("x.5        09 ");        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 ("Valid Starting Date" +t.getnotbefore ());        SYSTEM.OUT.PRINTLN ("Valid until 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]+ ","); }}}

The Java program implements the operation of the certificate

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.