Converting Between javax and java X509Certificates - Java Security

Java examples for Security:Certificate

Description

Converting Between javax and java X509Certificates

Demo Code

import java.io.ByteArrayInputStream;

public class Main {
  // Converts to java.security
  public static java.security.cert.X509Certificate convert(
      javax.security.cert.X509Certificate cert) {
    try {/* w ww.  java  2  s .co m*/
      byte[] encoded = cert.getEncoded();
      ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
      java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory
          .getInstance("X.509");
      return (java.security.cert.X509Certificate) cf.generateCertificate(bis);
    } catch (java.security.cert.CertificateEncodingException e) {
    } catch (javax.security.cert.CertificateEncodingException e) {
    } catch (java.security.cert.CertificateException e) {
    }
    return null;
  }

  // Converts to javax.security
  public static javax.security.cert.X509Certificate convert(
      java.security.cert.X509Certificate cert) {
    try {
      byte[] encoded = cert.getEncoded();
      return javax.security.cert.X509Certificate.getInstance(encoded);
    } catch (java.security.cert.CertificateEncodingException e) {
    } catch (javax.security.cert.CertificateEncodingException e) {
    } catch (javax.security.cert.CertificateException e) {
    }
    return null;
  }
}

Related Tutorials