使用BC库解密出现no such provider错误

时间:2021-08-25 18:59:29

使用BC库解密出现no such provider错误

错误提示如下:
Exception in thread "main" java.security.NoSuchProviderException: no such provider: BC
at sun.security.jca.GetInstance.getService(GetInstance.java:83)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
at java.security.KeyFactory.getInstance(KeyFactory.java:211)
解决方式
1. edit jre\lib\security\java.security
add security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
2. copy bc*.jar to jre\lib\ext
  • 但是上面这种方式并不在某些情况下不适用,又找到了另外一个方式,在代码里用到的地方前添加如下代码:
if (Security.getProvider("BC") == null) {
Security.addProvider(new BouncyCastleProvider());
} or Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());