Friday, 9 February 2018

Convert certificate into base64 encoded array

In my previous post, I explained how to create and export a self-signed certificate using ‘keytool’ command. In this post, I am going to show you how to convert the certificate into base64 encoded string.

To run this program, you need to create a self-signed certificate and pass the path of the file as an input to the application Test.java.

CertificateUtil.java
package com.sample.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Base64;
import java.util.Base64.Encoder;

public class CertificateUtil {
 private static final int BUFFER_SIZE = 65535;

 /**
  * 
  * 
  * @param certificateFilePath
  * @return the base64 encoded array from given certificate content.
  * 
  * @throws Exception
  */
 public static byte[] convertCertificateToByteArray(String certificateFilePath) throws Exception {
  if (certificateFilePath == null || certificateFilePath.isEmpty()) {
   throw new Exception("certificateFilePath should not be null or empty");
  }

  File file = new File(certificateFilePath);

  if (!file.exists()) {
   throw new Exception("File not exist : " + file.getAbsolutePath());
  }

  if (file.isDirectory()) {
   throw new Exception("File shouldn't be a directory : " + file.getAbsolutePath());
  }

  try (InputStream inputStream = new FileInputStream(file)) {

   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   byte[] buffer = new byte[BUFFER_SIZE];
   int numberOfReadBytes;

   while ((numberOfReadBytes = inputStream.read(buffer)) > 0) {
    byteArrayOutputStream.write(buffer, 0, numberOfReadBytes);
   }

   byte[] certificateInBytes = byteArrayOutputStream.toByteArray();

   Encoder encoder = Base64.getEncoder();
   byte[] encodedBytes = encoder.encode(certificateInBytes);
   return encodedBytes;

  }
 }

 /**
  * Convert the certificate into base64 encoded string.
  * 
  * @param certificateFilePath
  * @return
  * @throws Exception
  */
 public static String convertCertificateToString(String certificateFilePath) throws Exception {

  byte[] encodedBytes = convertCertificateToByteArray(certificateFilePath);
  String str = new String(encodedBytes);

  return str;

 }
}

Test.java
package com.sample.test;

import com.sample.util.CertificateUtil;

public class Test {
 public static void main(String args[]) throws Exception {
  String certificateFilePath = "C:\\Users\\krishna\\myCert.cer";

  byte[] encodedBytes = CertificateUtil.convertCertificateToByteArray(certificateFilePath);

  for (byte b : encodedBytes) {
   System.out.print(b + ",");
  }
 }
}

Output
77,73,73,68,98,84,67,67,65,108,87,103,65,119,73,66,65,103,73,69,72,85,106,56,54,106,65,78,66,103,107,113,104,107,105,71,57,119,48,66,65,81,115,70,65,68,66,110,77,81,115,119,67,81,89,68,86,81,81,71,69,119,74,114,99,106,69,81,77,65,52,71,65,49,85,69,67,66,77,72,97,51,74,112,99,50,104,117,89,84,69,81,77,65,52,71,65,49,85,69,66,120,77,72,97,51,74,112,99,50,104,117,89,84,69,81,77,65,52,71,65,49,85,69,67,104,77,72,97,51,74,112,99,50,104,117,89,84,69,81,77,65,52,71,65,49,85,69,67,120,77,72,97,51,74,112,99,50,104,117,89,84,69,81,77,65,52,71,65,49,85,69,65,120,77,72,97,51,74,112,99,50,104,117,89,84,65,101,70,119,48,120,79,68,65,121,77,68,107,119,78,68,69,119,77,106,74,97,70,119,48,120,79,84,65,121,77,68,107,119,78,68,69,119,77,106,74,97,77,71,99,120,67,122,65,74,66,103,78,86,66,65,89,84,65,109,116,121,77,82,65,119,68,103,89,68,86,81,81,73,69,119,100,114,99,109,108,122,97,71,53,104,77,82,65,119,68,103,89,68,86,81,81,72,69,119,100,114,99,109,108,122,97,71,53,104,77,82,65,119,68,103,89,68,86,81,81,75,69,119,100,114,99,109,108,122,97,71,53,104,77,82,65,119,68,103,89,68,86,81,81,76,69,119,100,114,99,109,108,122,97,71,53,104,77,82,65,119,68,103,89,68,86,81,81,68,69,119,100,114,99,109,108,122,97,71,53,104,77,73,73,66,73,106,65,78,66,103,107,113,104,107,105,71,57,119,48,66,65,81,69,70,65,65,79,67,65,81,56,65,77,73,73,66,67,103,75,67,65,81,69,65,113,111,68,80,56,113,47,76,101,114,65,73,98,54,87,66,120,48,110,120,51,105,54,55,48,112,55,117,119,82,98,84,81,47,78,101,75,53,52,66,69,115,86,51,112,107,106,84,56,71,108,80,115,84,80,50,97,105,54,88,115,48,51,56,111,89,107,103,83,120,108,100,108,102,104,103,112,100,88,115,87,113,104,57,53,43,89,100,113,68,80,74,86,50,105,70,52,76,65,69,84,50,90,57,113,114,85,67,104,112,54,77,107,109,72,81,114,56,72,82,105,49,43,50,85,79,83,54,87,73,48,73,88,52,102,90,97,115,88,97,112,76,49,99,81,67,115,71,84,89,101,109,49,98,52,107,74,109,89,80,88,99,122,104,116,79,77,75,119,74,88,98,100,65,48,89,118,122,69,43,52,109,50,112,88,49,119,106,77,117,102,105,50,101,88,56,97,52,113,117,111,106,81,106,74,114,85,82,110,113,55,52,47,88,74,101,112,78,110,65,48,121,87,82,117,99,122,100,76,71,51,88,112,80,70,102,116,81,97,67,105,65,106,43,79,97,47,51,98,75,85,89,88,82,103,49,79,68,100,108,53,86,84,97,120,85,107,113,76,47,50,111,100,119,52,118,118,70,115,50,102,104,83,110,111,117,65,76,115,99,43,119,121,65,75,97,66,72,51,56,90,73,67,85,67,111,83,105,88,54,109,85,57,104,67,118,66,112,98,72,70,107,54,84,111,81,99,80,84,111,51,67,107,87,71,83,122,66,105,80,114,81,73,68,65,81,65,66,111,121,69,119,72,122,65,100,66,103,78,86,72,81,52,69,70,103,81,85,84,49,83,49,69,68,50,115,85,89,80,67,105,56,84,104,109,122,47,116,69,113,105,83,76,73,73,119,68,81,89,74,75,111,90,73,104,118,99,78,65,81,69,76,66,81,65,68,103,103,69,66,65,75,97,98,88,107,43,76,105,102,81,77,110,65,56,101,74,118,68,107,110,56,120,90,51,70,75,114,47,57,111,115,109,73,99,74,106,107,79,52,105,48,118,116,110,71,79,83,120,81,52,43,73,121,65,84,120,74,47,52,110,114,88,65,90,119,66,73,52,43,113,52,108,49,51,71,78,70,119,43,83,54,101,98,75,111,89,102,78,87,69,118,90,72,85,98,106,76,65,76,114,57,56,43,110,104,72,115,85,82,89,55,51,84,73,86,50,110,119,55,53,98,87,79,118,104,51,81,82,112,68,68,80,105,80,47,51,70,122,115,57,88,106,69,78,120,101,85,88,99,85,86,43,109,76,71,69,84,75,71,97,54,115,122,102,99,90,48,72,117,97,101,118,97,53,110,120,68,116,55,85,43,52,47,51,120,121,77,79,57,67,87,117,89,103,108,104,67,56,97,99,116,49,112,100,51,82,102,90,100,90,74,97,68,78,51,102,85,121,47,43,116,111,99,75,88,77,109,111,48,115,55,111,76,105,65,108,121,102,76,78,104,110,103,50,97,113,72,119,72,117,47,115,65,111,72,106,89,121,76,104,113,122,55,52,48,49,77,109,113,72,89,88,56,104,107,79,74,54,70,66,88,106,74,90,122,54,50,122,102,114,70,85,86,111,77,49,48,122,78,107,66,121,73,98,100,76,52,87,122,78,102,47,100,56,122,48,88,53,55,73,103,72,103,48,56,73,80,112,89,74,79,102,101,110,83,73,113,70,51,82,109,111,61,




No comments:

Post a Comment