title: How to decode a base64 encoded certificate
body: <p>Below is my requirement:</p>
  <ol>
  <li>Program will have an XML file as input with 3 tags: <code>OrgContent</code>, <code>Signature</code> and <code>Certificate</code>. All these data are Base64 encoded. Note: Program is using BC jars</li>
  <li>Program needs to decode them and verify the data for its authenticity using the signature and certificate</li>
  <li>Verified data should be Base64 decoded and written into another file</li>
  </ol>
  <p>Below is my code which tries to decode the certificate:</p>
  <pre><code>public void executeTask(InputStream arg0, OutputStream arg1) throws SomeException{
      try{
          BufferedReader br = null;
          br = new BufferedReader(new InputStreamReader(arg0));
          String orgContent  = &quot;&quot;, splitData = &quot;&quot;, signContent = &quot;&quot;, certContent = &quot;&quot;;
              
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          Document doc = docBuilder.parse(arg0);
          doc.getDocumentElement().normalize();
              
          NodeList originalContent = doc.getElementsByTagName(&quot;OrgContent&quot;);
          Element originalElement = (Element)originalContent.item(0);
          NodeList textOrgContent = originalElement.getChildNodes();
          orgContent = ((Node)textOrgContent.item(0)).getNodeValue().trim();
              
          NodeList signature = doc.getElementsByTagName(&quot;Signature&quot;);
          Element signatureElement = (Element)signature.item(0);
          NodeList signatureContent = signatureElement.getChildNodes();
          signContent = ((Node)signatureContent.item(0)).getNodeValue().trim();
              
          NodeList certificate = doc.getElementsByTagName(&quot;Certificate&quot;);
          Element certificateElement = (Element)certificate.item(0);
          NodeList certificateContent = certificateElement.getChildNodes();
          certContent = ((Node)certificateContent.item(0)).getNodeValue().trim();
          String decodedCertContent = new String(Base64.decode(certContent),StandardCharsets.UTF_8);
          byte[] certByteValue = Base64.decode(certContent);
          CertificateFactory certFactory = CertificateFactory.getInstance(&quot;X.509&quot;);
          System.out.println(&quot;certContent:\n&quot; + new String(certByteValue,StandardCharsets.UTF_8));
          InputStream inputStream = new ByteArrayInputStream(Base64.decode(certContent));
              
          X509Certificate cert = (X509Certificate)certFactory.generateCertificate(inputStream);
          
          arg1.write(decodedOrgData.getBytes());
          arg1.flush();   
      }
      catch (ParserConfigurationException e){
          e.printStackTrace();
      }
      catch (IOException e){
          e.printStackTrace();
      }
      catch (org.xml.sax.SAXException e){
          e.printStackTrace();
      }
      catch (CertificateException e){
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>When I print the value of new String(certByteValue,StandardCharsets.UTF_8) the program is printing some unrecognizable text. When executing the last line of the code <code>X509Certificate cert = (X509Certificate)certFactory.generateCertificate(inputStream);</code> system is throwing</p>
  <blockquote>
  <p>java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: Invalid BER/DER data (too huge?).</p>
  </blockquote>
  <p>Since I am a newbie to these certificates thing, I have hit a deadlock. I am unable to proceed with the requirement. I would like to know how to achieve my above said requirements.</p>
  <p>The input stream to the above code will be an XML file. Another program creates that XML file with base64 encoded data with signature and certificate. In that program, for encoding the certificate the below code is used:</p>
  <pre><code>KeyStore keyStore = KeyStore.getInstance(&quot;JKS&quot;);
  keyStore.load(new FileInputStream(&quot;Filepath/certificate.p12&quot;), &quot;password&quot;.toCharArray());
  PrivateKey privateKey = (PrivateKey)keyStore.getKey(alias, &quot;password&quot;.toCharArray());
  
  CertificateFactory factory = CertificateFactory.getInstance(&quot;X.509&quot;);
  X509Certificate certificate = (X509Certificate) factory.generateCertificate(new FileInputStream(&quot;D:/Sujai/Implementation Team/PI/Axis Treds/Certificates/PI_7.5_Cert/Arteria_Certificate-cert.cert&quot;));
  byte[] encodedCert = certificate.getEncoded();
  String encodedStringCert = new String(Base64.encode(new String(encodedCert).getBytes(StandardCharsets.UTF_8)));
  </code></pre>
  <p>The variable <code>encodedStringCert</code> is passed as the certificate value inside a tag. In the program shared at the top of this question, I need to decode this certificate value.</p>
  <p>Sample certificate content:</p>
  <pre class="lang-none prettyprint-override"><code>-----BEGIN CERTIFICATE-----
  MIIDBjCCAe6....IM1g==
  -----END CERTIFICATE-----
  </code></pre>