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