Wednesday, 14 February 2018

Get x.509 certificate from encoded string

Step 1: Decode the encoded string.

Decoder decoder = Base64.getDecoder();
byte[] decodedData = decoder.decode(encodedString);

Step 2: Get input stream from decoded data.
InputStream inputStream = new ByteArrayInputStream(decodedData)

Step 3: Generate certificate from decoded data.
CertificateFactory cf = CertificateFactory.getInstance("X.509");

java.security.cert.Certificate certificate = cf.generateCertificate(inputStream);

if (certificate instanceof X509Certificate) {
         return (X509Certificate) certificate;
}

Find the below working application.

CertificateUtil.java
package com.sample.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.Base64.Decoder;;

public class CertificateUtil {
 /**
  * 
  * @param encodedString
  *            base64 encoded string
  * 
  * @return X509Certificate from the encodedString, null on failure cases
  */
 public static X509Certificate getX509Certificate(String encodedString) {
  if (encodedString == null) {
   return null;
  }

  Decoder decoder = Base64.getDecoder();
  byte[] decodedData = decoder.decode(encodedString);

  try (InputStream inputStream = new ByteArrayInputStream(decodedData)) {
   CertificateFactory cf = CertificateFactory.getInstance("X.509");

   java.security.cert.Certificate certificate = cf.generateCertificate(inputStream);

   if (certificate instanceof X509Certificate) {
    return (X509Certificate) certificate;
   }

  } catch (Exception e) {
   e.printStackTrace();
  }

  return null;

 }
}

Test.java
package com.sample.test;

import java.security.cert.X509Certificate;

import com.sample.util.CertificateUtil;

public class Test {
 
 public static void main(String args[]) throws Exception {
  String encodedString = "MIIDbTCCAlWgAwIBAgIEHUj86jANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJrcjEQMA4GA1UECBMHa3Jpc2huYTEQMA4GA1UEBxMHa3Jpc2huYTEQMA4GA1UEChMHa3Jpc2huYTEQMA4GA1UECxMHa3Jpc2huYTEQMA4GA1UEAxMHa3Jpc2huYTAeFw0xODAyMDkwNDEwMjJaFw0xOTAyMDkwNDEwMjJaMGcxCzAJBgNVBAYTAmtyMRAwDgYDVQQIEwdrcmlzaG5hMRAwDgYDVQQHEwdrcmlzaG5hMRAwDgYDVQQKEwdrcmlzaG5hMRAwDgYDVQQLEwdrcmlzaG5hMRAwDgYDVQQDEwdrcmlzaG5hMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqoDP8q/LerAIb6WBx0nx3i670p7uwRbTQ/NeK54BEsV3pkjT8GlPsTP2ai6Xs038oYkgSxldlfhgpdXsWqh95+YdqDPJV2iF4LAET2Z9qrUChp6MkmHQr8HRi1+2UOS6WI0IX4fZasXapL1cQCsGTYem1b4kJmYPXczhtOMKwJXbdA0YvzE+4m2pX1wjMufi2eX8a4quojQjJrURnq74/XJepNnA0yWRuczdLG3XpPFftQaCiAj+Oa/3bKUYXRg1ODdl5VTaxUkqL/2odw4vvFs2fhSnouALsc+wyAKaBH38ZICUCoSiX6mU9hCvBpbHFk6ToQcPTo3CkWGSzBiPrQIDAQABoyEwHzAdBgNVHQ4EFgQUT1S1ED2sUYPCi8Thmz/tEqiSLIIwDQYJKoZIhvcNAQELBQADggEBAKabXk+LifQMnA8eJvDkn8xZ3FKr/9osmIcJjkO4i0vtnGOSxQ4+IyATxJ/4nrXAZwBI4+q4l13GNFw+S6ebKoYfNWEvZHUbjLALr98+nhHsURY73TIV2nw75bWOvh3QRpDDPiP/3Fzs9XjENxeUXcUV+mLGETKGa6szfcZ0Huaeva5nxDt7U+4/3xyMO9CWuYglhC8act1pd3RfZdZJaDN3fUy/+tocKXMmo0s7oLiAlyfLNhng2aqHwHu/sAoHjYyLhqz7401MmqHYX8hkOJ6FBXjJZz62zfrFUVoM10zNkByIbdL4WzNf/d8z0X57IgHg08IPpYJOfenSIqF3Rmo=";
  X509Certificate x509Certificate = CertificateUtil.getX509Certificate(encodedString);
  
  System.out.println("Issuer Name : " + x509Certificate.getIssuerX500Principal());
  
 }
}

Output
Issuer Name : CN=krishna, OU=krishna, O=krishna, L=krishna, ST=krishna, C=kr


No comments:

Post a Comment