I have a X509Certificate and I write/print it to a file as follows. (I'm not writing encoded bytes, because I want to read the content of the certicate)
X509Certificate cer = generateCertificate(); // cer is DER encoded
writeToFile( cer.toString() ); // cer.toString() converts DER to UTF/ASCII???
Later I want to read this file (above) as String and create a new X509Certificate.
String cerStr = readCerFromFile(); // Read what is written above. In ASCII/ UTF format
ByteArrayInputStream bais = null;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
bais = new ByteArrayInputStream(cerStr.getBytes());
return (X509Certificate) cf.generateCertificate(bais);
} ...
This throws following Exception.
Java.security.cert.CertificateParsingException: Invalid DER-encoded certificate data
And it is obvious that I'm not converting cerStr to DER format (and I don't know whether it is possible to convert into DER ). Can any one please explain how can create an X509Certicate from a String which is not encoded.
Thanks in advance.!