当我尝试使用证书的字符串转换,引发异常 [英] When I try to convert a string with certificate, Exception is raised
问题描述
我有SIGNES文档,并将文档发送,标志和证书到服务器端的小程序。在服务器端的portlet接收这3个文件,所有文件都存储在Base64格式,但是当我尝试获得证书它会引发异常
I have an applet which signes document, and sends a document, sign, and certificate to the server side. On the server side portlet receives these 3 files, all files are stored in base64 format, but when I try to get certificate it raises exception
java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: Empty input
at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:104)
小程序一侧code:
applet side code:
public static byte[] certificate;
public static String getCertificateString() {
String str = "";
byte[] result = null;
result = Base64.encode(certificate);
for (int i = 0; i < result.length; i++) {
str += (char) (result[i]);
}
return str;
}
//initialization of certificate from the store
Certificate cert = store.getCertificate(aliasKey);
certificate = cert.toString().getBytes();
在这之后我送证到Portlet,其中需要验证标志。但该证书转换失败。
after this I send certificate to the portlet, where need to verify the sign. But the certificate conversion is failed.
的portlet code:
portlet code:
String certificate = request.getParameter("cert");
byte[] cert_array = Base64.decode(certificate.getBytes());
try {
cert = CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(cert_array));
}catch(Exception e){
e.printStackTrace();
}
在这一点上,在try块,异常引发
And at this point, in the try block, Exception is raised
推荐答案
好吧,@ test1604您尝试这样的事情,是实现X509TrustManager类,确定在这里,我们去:
Ok, @test1604 you try something like this, is implements X509TrustManager class, ok here we go:
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class YouNameClass implements X509TrustManager {...
public YouNameClass() {
super();
}
}
和添加此方法,
private static void trustAllHttpsCertificates() throws Exception {
// Create a trust manager that does not validate certificate chains:
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new YouNameClass();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
和方法重写:
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return;
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
就是这样。 :)
That's it. :)
这篇关于当我尝试使用证书的字符串转换,引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!