Java security: get certificate from string (byte array) 

Joined:
07/27/2010
Posts:
130

May 23, 2011 12:07:59    Last update: May 23, 2011 12:09:42
You can construct a java.security.cert.Certificate object from a string or byte array. Byte array is cleaner because for string you also have to know the character encoding.
// import java.security.cert.CertificateFactory;
// import java.security.cert.Certificate;
String certString = "some string representing a X.509 certificate";
Certificate myCert = CertificateFactory
				     .getInstance("X509")
				     .generateCertificate(
                                        // string encoded with default charset
					new ByteArrayInputStream(certString.getBytes())
				     );

If the certificate string is encoded in "UTF-8", the ByteArrayInputStream should be created with:
new ByteArrayInputStream(certString.getBytes("UTF-8"));
Share |
| Comment  | Tags