Advertisement
ratacheski

Certificado A1 exemplo

Nov 1st, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package br.com.javac.util;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.security.KeyStore;
  7. import java.security.Provider;
  8. import java.security.cert.X509Certificate;
  9. import java.util.Enumeration;
  10.  
  11. public class DadosCertificadoA1 {
  12.  
  13.     @SuppressWarnings("static-access")
  14.     public static void main(String[] args) {
  15.        
  16.         try {
  17.             String caminhoDoCertificadoDoCliente = "C:/JavaC/NF-e/certificadoDoCliente.pfx";
  18.             String senhaDoCertificadoDoCliente = "1234";           
  19.            
  20.             InputStream entrada = new FileInputStream(caminhoDoCertificadoDoCliente);
  21.             KeyStore ks = KeyStore.getInstance("pkcs12");
  22.             try {
  23.                 ks.load(entrada, senhaDoCertificadoDoCliente.toCharArray());
  24.             } catch (IOException e) {
  25.                 throw new Exception("Senha do Certificado Digital incorreta ou Certificado inválido.");
  26.             }
  27.            
  28.             Provider pp = ks.getProvider();
  29.             info("--------------------------------------------------------");
  30.             info("Provider   : " + pp.getName());
  31.             info("Prov.Vers. : " + pp.getVersion());
  32.             info("KS Type    : " + ks.getType());
  33.             info("KS DefType : " + ks.getDefaultType());
  34.    
  35.             String alias = null;
  36.             Enumeration <String> al = ks.aliases();
  37.             while (al.hasMoreElements()) {
  38.                 alias = al.nextElement();
  39.                 info("--------------------------------------------------------");
  40.                 if (ks.containsAlias(alias)) {
  41.                     info("Alias exists : '" + alias + "'");
  42.                    
  43.                     X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
  44.                     info("Certificate  : '" + cert.toString() + "'");
  45.                     info("Version      : '" + cert.getVersion() + "'");
  46.                     info("SerialNumber : '" + cert.getSerialNumber() + "'");
  47.                     info("SigAlgName   : '" + cert.getSigAlgName() + "'");
  48.                     info("NotBefore    : '" + cert.getNotBefore().toString() + "'");
  49.                     info("NotAfter     : '" + cert.getNotAfter().toString() + "'");
  50.                     info("TBS          : '" + cert.getTBSCertificate().toString() + "'");
  51.                 } else {
  52.                     info("Alias doesn't exists : '" + alias + "'");
  53.                 }
  54.             }
  55.         } catch (Exception e) {
  56.             error(e.toString());
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * Log ERROR.
  62.      *
  63.      * @param error
  64.      */
  65.     private static void error(String error) {
  66.         System.out.println("| ERROR: " + error);
  67.     }
  68.  
  69.     /**
  70.      * Log INFO.
  71.      *
  72.      * @param info
  73.      */
  74.     private static void info(String info) {
  75.         System.out.println("| INFO: " + info);
  76.     }
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement