// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.mail.MessagingException;
import javax.security.cert.CertificateEncodingException;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import javax.xml.bind.DatatypeConverter;

public class X509ExpImp {
    public static String encode(X509Certificate cert) throws CertificateEncodingException, MessagingException, IOException {
        return DatatypeConverter.printBase64Binary(cert.getEncoded());
    }
    public static X509Certificate decode(String s) throws CertificateException, MessagingException, IOException {
        return X509Certificate.getInstance(DatatypeConverter.parseBase64Binary(s));
    }
    public static void main(String[] args) throws CertificateException, IOException, MessagingException {
        InputStream is = new FileInputStream("/work/AV.cert");
        X509Certificate cert = X509Certificate.getInstance(is);
        is.close();
        System.out.println(cert.getSubjectDN().getName());
        System.out.println(cert.getSigAlgName());
        String s = encode(cert);
        System.out.println(s.length());
        X509Certificate cert2 = decode(s);
        System.out.println(cert2.getSubjectDN().getName());
        System.out.println(cert2.getSigAlgName());
    }
}
