Saturday, 10 February 2018

Convert base64 encoded string to certificate

In my previous post, I explained how to convert the certificate to base64 encoded string. In this post, I am going to show you how to convert the encoded string to certificate.

It is very simple, just decode the encoded string and write the encoded string to the file.

Test.java
package com.sample.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Base64.Decoder;

public class Test {
 private static final String certificateFilePath = "C:\\Users\\krishna\\myCertificate.cer";

 public static void writeBytesToFile(String encodedString, String certificateFilePath)
   throws FileNotFoundException, IOException {
  byte[] encodedBytes = encodedString.getBytes();
  Decoder decoder = Base64.getDecoder();
  byte[] decodedBytes = decoder.decode(encodedBytes);

  try (FileOutputStream fos = new FileOutputStream(certificateFilePath)) {
   fos.write(decodedBytes);
  }
 }

 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=";
  writeBytesToFile(encodedString, certificateFilePath);
 }
}

When you ran above application, you can able to see below certificate.

You may like


No comments:

Post a Comment