Java certificate operations

Source: Internet
Author: User

1 Java program reads the certificate from the 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 the certificate FileOutputStream fout = new FileOutputStream ("tmp.txt"); BufferedWriter out = new BufferedWriter (new OutputStreamWriter (fout); out. write (s, 0, s. length (); out. close () ;}2 Java program directly reads the certificate from the 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 the Java program displays the specified certificate 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" + t. getVersion (); System. out. println ("Serial Number" + t. getSerialNumber (). toString (16); System. out. println ("Full name" + t. getSubjectDN (); System. out. println ("issuer's full name n" + t. getIssuerDN (); System. out. println ("validity period start date" + t. getNotBefore (); System. out. println ("validity period ends on" + 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] + ",");}}}

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.