Exception in thread "main" java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.
at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:109)
at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:305)
at com.yitong.commons.util.security.RSACerPlus.doEncrypt(RSACerPlus.java:105)
at com.yitong.commons.util.security.RSACerPlus.main(RSACerPlus.java:145)
Caused by: java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.
at sun.security.util.DerInputStream.getLength(DerInputStream.java:544)
at sun.security.util.DerValue.init(DerValue.java:346)
at sun.security.util.DerValue.<init>(DerValue.java:302)
at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:104)
... 3 more
做实现对数据进行加密处理的方法时候报出以上异常,查询了一下似乎和数字证书有关.但还是感觉一头雾水.求各位大神帮忙.下面是代码:
public String doEncrypt(String[] str) throws Exception {
//获取公钥
CertificateFactory cff = CertificateFactory.getInstance("X.509");
String filePath = Log4jInit.getCommString(Consts.jks_path);
InputStream in = new FileInputStream(filePath);// 证书文件
//获得公钥文件(报错的地方)
Certificate cf = cff.generateCertificate(in);
PublicKey pk1 = cf.getPublicKey(); // 得到证书文件携带的公钥
Cipher cipher = Cipher.getInstance("RSA");
// 加密模式
cipher.init(Cipher.ENCRYPT_MODE, pk1);
byte[] msg1 = cipher.doFinal(str.toString().getBytes("UTF-8")); // 加密后的数据
return new BASE64Encoder().encode(msg1);
}
------解决方案--------------------
长度不对,RSA对长度有要求,比如你用asdfg进行加密,而解密的长度不符合要求,比如asdf,就会出现这个错误。