Reputation: 1
I am getting the below exception when I am trying to transfer the certificate to X.509
"Could not parse certificate: java.io.IOException: Unsupported encoding"
The 2048 certificate from CA is like this(the certicicate is just one line which has no linefeed) ,is this normal?
-----BEGIN CERTIFICATE----- MIIERTCCAy2gAwIBAgIFIBkicgAwDQYJKoZIhvcNAQEFBQAwWTELMAkGA1UEBhMCQ04xMDAuBgNVBAoTJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEYMBYGA1UEAxMPQ0ZDQSBURVNUIE9DQTExMB4XDTE3MDQwNjA2NDQ1OVoXDTE5MDQwNjA2NDQ1OVowdzELMAkGA1UEBhMCQ04xFTATBgNVBAoTDENGQ0EgVEVTVCBDQTERMA8GA1UECxMITG9jYWwgUkExGTAXBgNVBAsTEE9yZ2FuaXphdGlvbmFsLTExIzAhBgNVBAMUGjA1MUBDSl9BQUJCQ0BaMDAwMDAwMDU1MUAyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtgAftvnIcaGITXhuWY1R1IYfrCstmDKJYO9anhFcUrsEe7l1BHcmXeF1Fr7KzTr+XmQx8RkSrECm6Gyfg/jElmh6XHEL3UyExIuSxuCoTs+71JlBPkCDcTdyo4lDI6Ox3JsKU/W8LFKSpae6jAA0uhgwjPCI5uizONLWTdRiXRNRUG7zvGaCgHDipP7gN1Tm+KvlDs8xJGtLdeErlIIDy4Tw2EkK+LD+L9FD8CruzpY52UkTfYfqM3Mwu1EZtfIke0M/dpSkP+46XPJH7CCbXv+f65ST2Uh+PclwPo3O2raZMZ2R3alCEMx0ZKYbA4hekdt/FJygUwN9e1MCu8KqyQIDAQABo4H1MIHyMB8GA1UdIwQYMBaAFPwLvESaDjGhg6mBhyceBULGv1b4MEgGA1UdIARBMD8wPQYIYIEchu8qAQIwMTAvBggrBgEFBQcCARYjaHR0cDovL3d3dy5jZmNhLmNvbS5jbi91cy91cy0xNS5odG0wOgYDVR0fBDMwMTAvoC2gK4YpaHR0cDovLzIxMC43NC40Mi4zL09DQTExL1JTQS9jcmwxODExOS5jcmwwCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBTNUgad4XedWMDOeezrG+J+iwFxQDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwQwDQYJKoZIhvcNAQEFBQADggEBAI906RKm5xPIaJzIeVpXRUIyHtf/9klxqNdjoXbwfplSocUioYZDPOy33w2b9Wfl/HDNKVUksv6n09dx0hdwiJZaNaVbLw0VGPwg5eTEcSEj1iQzkGGlsCt8uNROGilB8Vn0cPfhByroFO9e8bj7GNAxYtg7fvUB9XIXlMqLukIZ5JY6yOLj2y/MvoQp7B/9xdBWxqDqWhZaaEgsRxeezZ3CIGQjevSF/xNe8g2zM/13K38h64FGS3P1iNAcyJUfjoMSAXMSgPWE8uPDtENU+XgthSsNrTPBxkkY5ZuEj7YGHsiHt8mAK37QigC12fL1gF7OG8oCE/a1ZyfTukj0KRI=-----END CERTIFICATE-----
The program is like this:
public static X509Certificate getX509Certificate(String base64Cert)
{
X509Certificate cert = null;
if (!isNullOrEmpty(base64Cert))
{
log.error("getX509Certificate(): " + base64Cert);
BufferedInputStream is = new BufferedInputStream(
new ByteArrayInputStream(base64Cert.getBytes()));
CertificateFactory cf;
try
{
cf = CertificateFactory.getInstance("X.509");
cert = (X509Certificate) cf.generateCertificate(is);
} catch (CertificateException e)
{
log.error("cert format error,cert content is [" + base64Cert
+ "]");
}
} else
{
log.error("cert is null");
}
return cert;
}
any help would be appreciated !.
Upvotes: 0
Views: 10273
Reputation: 198
This is my solution to convert Base64 formatted certificate to X509Certificate
byte[] decodedCertificate =
Base64.decodeBase64(encodedCertificate.getBytes());
CertificateFactory certificateFactory;
Certificate certificate = null;
try {
certificateFactory = CertificateFactory.getInstance("X.509");
certificate = certificateFactory.generateCertificate(new
ByteArrayInputStream(decodedCertificate));
} catch (CertificateException e) {
e.printStackTrace();
}
X509Certificate x509Certificate = (X509Certificate) certificate;
Upvotes: 1
Reputation: 33238
The PEM file format is
The newlines are a part of the format. Without it, you're not PEM, and since it presumably only supports PEM and DER (the binary representation of the base64-encoded data), and your value was neither of those, it was in some "unsupported encoding", which happened to be "PEM-like, but without newlines"
Upvotes: 1
Reputation: 1
Anyway,this is my solution ,transfer the singleline cert to multiline . but i still don't know the origin reason ,anyone can explain this ,please Post your answer.
public static String singleLine2MultiLine(String p10) throws Exception{
byte[] base64Data = p10.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(base64Data);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte tmp;
while ((tmp = (byte) bis.read()) != -1) {
if (tmp != 0x0a && tmp != 0x0d) {
bos.write(tmp);
}
}
byte[] bSingleLine = bos.toByteArray();
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
for (int i = 0; i < bSingleLine.length; i++) {
if (i != 0 && i % 64 == 0) {
bos2.write("\r\n".getBytes());
}
bos2.write(bSingleLine[i]);
if (i == bSingleLine.length - 1) {
bos2.write("\r\n".getBytes());
}
}
return new String(bos2.toByteArray());
}
Upvotes: 0