在开发java使用RSA加密是遇到这个问题,
Exception in thread "main" java.lang.SecurityException: JCE cannot authenticate the provider BC
at javax.crypto.Cipher.getInstance(Cipher.java:657)
at com.suntien.hr.common.util.RSAUtils.encryptByPublicKey(RSAUtils.java:102)
at com.suntien.hr.common.util.RSAUtils.main(RSAUtils.java:256)
Caused by: java.util.jar.JarException: file:/D:/Workspaces/MyEclipse2016/HR/WebRoot/WEB-INF/lib/fr-third-8.0.jar has unsigned entries - .classpath
at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:500)
at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:361)
at javax.crypto.JarVerifier.verify(JarVerifier.java:289)
at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:159)
at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:185)
at javax.crypto.Cipher.getInstance(Cipher.java:653)
... 2 more
于是百度了各路大神方法,解决方案如下:
1. 在java下找到 jre/lib/security/java.security,添加一行代码:
security.provider.9=org.bouncycastle.jce.provider.BouncyCastleProvider //这里的9,视具体情况而定,你可能是是10,11...
2.添加2个扩展包到jre/lib/ext目录下:bcprov-jdk15-135.jar bcprov-jdk16-143.jar
3.把你的class path中的有的BC的扩展包都删除
4.说maven会解压包导致jar包的签名不能使用,所以放在jre/lib/ext目录下,并使用Security.addProvider(new BouncyCastleProvider())添加Provider。
参考文章:
a、http://bbs.csdn.net/topics/390690044?list=lz,这个跟我出现的情况大致相同,但是没有答案。。。。
b、https://stackoverflow.com/questions/22160780/java-jce-cannot-authenticate-provider-bc-in-jarsplice-created-jar# ,这个试了下,但是全英文的也是有点蒙
c、http://blog.csdn.net/haihaa/article/details/77929874
d、https://blog.csdn.net/qq_15068711/article/details/54691934
最终通过方案4解决了。