日期:2014-05-20 浏览次数:20982 次
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);
}