前言

相信搞过加解密的同学大部分都会遇到过这个问题——JCE cannot authenticate the provider BC
笔者最近在做一个亚马逊的项目需要进行GPG加解密,因为测试jar包是亚马逊提供的,jar是经过签名的,咱也不能修改,所以只能修改自己的JDK配置

分析

在解密时错误日志如下:

Exception in thread "main" java.lang.IllegalArgumentException: Could not decrypt the provided file, please check that provided keys are correct and in the format as specified in tech spec.Exception decrypting keyat com.amazon.psp.datavalidation.file.FileUtils.getDataAsStream(FileUtils.java:37)at com.amazon.psp.datavalidation.entrypoint.MainClass.main(MainClass.java:22)
Caused by: org.bouncycastle.openpgp.PGPException: Exception decrypting keyat org.bouncycastle.openpgp.PGPSecretKey.extractKeyData(Unknown Source)at org.bouncycastle.openpgp.PGPSecretKey.extractPrivateKey(Unknown Source)at org.bouncycastle.openpgp.PGPSecretKey.extractPrivateKey(Unknown Source)at org.bouncycastle.openpgp.PGPSecretKey.extractPrivateKey(Unknown Source)at com.amazon.psp.datavalidation.pgp.PgpDecryptionHelper.decryptAndVerifySignature(PgpDecryptionHelper.java:68)at com.amazon.psp.datavalidation.file.FileUtils.getDataAsStream(FileUtils.java:26)... 1 more
Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BCat javax.crypto.Cipher.getInstance(Cipher.java:656)at org.bouncycastle.jcajce.ProviderJcaJceHelper.createCipher(Unknown Source)at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createCipher(Unknown Source)at org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder$1.recoverKeyData(Unknown Source)... 7 more
Caused by: java.util.jar.JarException: file:/Users/xxxxx/Desktop/case/PSPPTestPackage-1.8.jar has unsigned entries - ATTRIBUTIONS.txtat javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:502)at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:363)at javax.crypto.JarVerifier.verify(JarVerifier.java:289)at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:164)at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:190)at javax.crypto.Cipher.getInstance(Cipher.java:652)... 10 more

看到这个不用慌,因为只是缺少一个jar而已,添加一个jar,修改一下JDK安全配置就搞定。

这里提醒一句:如果修改后不生效,记得多换几个版本!!!

大家可以看到bouncycastle包抛出的异常,我们可以去maven仓库https://mvnrepository.com/search?q=bouncycastle下载一下这个jar,搜索后会出现如下页面。

maven配置如下,因为我是通过maven来下载这个jar的,你要是有其他渠道可以拿到jar就可以不用我这种方式。

    <dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.50</version></dependency>

我们选择第3个,至于为什么选择它,稍后再说。

修改JDK配置

找到自己本地JDK安装根目录,进入jre/lib/security目录,编辑java.security文件; 操作如下:

# 因为我是Linux和MacOS记得要加 sudo ,windows下直接编辑内容即可# 进入JDK目录
cd /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/security# 编辑security文件
sudo vim java.security

打开文件后,添加如下配置:

# 这个序号11根据自己的配置写就行,有可能你的配置和我不一样
security.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider

人家本身是有其他安全配置的,添加完咱们的配置后,配置长这样:

# There must be at least one provider specification in java.security.
# There is a default provider that comes standard with the JDK. It
# is called the "SUN" provider, and its Provider subclass
# named Sun appears in the sun.security.provider package. Thus, the
# "SUN" provider is registered via the following:
#
#    security.provider.1=sun.security.provider.Sun
#
# (The number 1 is used for the default provider.)
#
# Note: Providers can be dynamically registered instead by calls to
# either the addProvider or insertProviderAt method in the Security
# class.#
# List of providers and their preference orders (see above):
#
security.provider.1=sun.security.provider.Sun
security.provider.2=sun.security.rsa.SunRsaSign
security.provider.3=sun.security.ec.SunEC
security.provider.4=com.sun.net.ssl.internal.ssl.Provider
security.provider.5=com.sun.crypto.provider.SunJCE
security.provider.6=sun.security.jgss.SunProvider
security.provider.7=com.sun.security.sasl.Provider
security.provider.8=org.jcp.xml.dsig.internal.dom.XMLDSigRI
security.provider.9=sun.security.smartcardio.SunPCSC
security.provider.10=apple.security.AppleProvider
security.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider
#
# Sun Provider SecureRandom seed source.

截图如下:

添加依赖

将刚才下载好的jar文件( bcprov-jdk15on-1.50.jar ),复制到jre/lib/ext/目录下

然后就OK了!

至于刚才说为啥知道下载这个jar,是因为亚马逊的解密的代码用的版本是1.50,而只有org.bouncycastle » bcprov-jdk15on跟我的版本一致!

另外扩展一个思路:如果你不知道第三方提供的解密工具类是哪个版本,尝试去反编译,看下它的源码,看看打包出的jar是哪年的?
然后对应maven仓库里的依赖找到对应的年份。

加解密遇到的JCE cannot authenticate the provider BC问题解决方案相关推荐

  1. 升级jdk版本后,出现SecurityException: JCE cannot authenticate the provider BC

    先分析异常的描述: JCE cannot authenticate the provider BC:在使用带签名的jar包时,java会对jar包的签名进行验证,以确认jar包是不是合法的. Secu ...

  2. JCE问题 :error constructing MAC: java.lang.SecurityException: JCE cannot authenticate the provider BC

    1.报错: <dependency><groupId>com.cybersource</groupId><artifactId>cybersource- ...

  3. JCE cannot authenticate the provider BC

    JCE cannot authenticate the provider BC 这个问题,很多人遇到,如下 本人本着求真溯源的态度,先后通道搜索国内外资料和官网文档的方式,顺道撸了一点JCE.最终得到 ...

  4. linux上java解加密(AES/CBC)异常:java.lang.SecurityException: JCE cannot authenticate the provider BC办法

    对接第三方厂商需求时,需要对数据AES256进行解密,由于java本身不支持,需要添加依赖. 文章目录 一.版本适配 1. 版本对应关系 2. maven仓库地址 3. maven坐标 二.linux ...

  5. 解决JCE cannot authenticate the provider BC

    报错原因:JAR包必须签署特殊的证书才能使用 解决: 1.在java下找到  jre/lib/security/java.security,添加一行代码: security.provider.9=or ...

  6. 运行jar包出现问题:java.lang.SecurityException: JCE cannot authenticate the provider BC

    解决方法: 1.在\jre1.8.0_131\lib\security这个文件里找到'java.security',添加security.provider.x=org.bouncycastle.jce ...

  7. JCE cannot authenticate the provider BC问题解决

    报错原因: 在使用oracle的JDK时,JAR包必须签署特殊的证书才能使用.(具体是什么协议没查出来,惭愧) 方案一: 使用openJDK或者非oracle的JDK,这样就可以绕开证书的限制.该方案 ...

  8. Decipher加密工具报错:JCE cannot authenticate the provider BC 解决方法

    原因:这个大可不必想的太复杂,从字面上看,是缺了一个名为BC 的provider 解决方法:加上缺失的provider即可 步骤: 1. 在java安装目录下找到  jre/lib/security/ ...

  9. JCE cannot authenticate the provider BC问题已解决

    这里不说问题原因了(主要是懒),这里只说解决方法,附带相关的包. 1. 找到 java.security 在jvm安装地方 /path_to_your_jvm/jre/lib/security(jav ...

最新文章

  1. python 三角函数
  2. python发明者叫什么-python发明人
  3. 记录-JQuery日历插件My97DatePicker日期范围限制
  4. Sqlserver 查询语句性能测试
  5. OLAP引擎:基于Druid组件进行数据统计分析
  6. yum方式安装mysql多实例_centos7下使用yum安装mysql
  7. 在html中写三角,css3怎么写三角形?
  8. 复变函数系列(三 ) - 复变函数的积分
  9. protobuf使用错误总结
  10. python粘贴代码到word_python怎么粘贴代码
  11. html答题游戏代码,html5+css3+ajax手机端脑筋急转弯答题游戏代码
  12. c++设计地铁售票系统_地铁自动售票机中票务系统的设计与开发
  13. 哪些产品需要过3C认证?3C认证具体清单
  14. 计算机的硬盘如何查看,怎样查看电脑硬盘信息 电脑中的硬盘信息
  15. 华为6个月试用期经历
  16. linux hd4000显卡驱动,AMD Radeon HD 2000/HD 3000/HD 4000系列显卡驱动怎么样
  17. T检验 ANOVA
  18. 【Python】如何判断丑数
  19. 单页活动页面html,优秀HTML5活动页面
  20. 内容算法解读:提高内容摘要与原文的一致性(Faithfulness)

热门文章

  1. gRPC和gRPC-Gateway的使用以及遇到的坑
  2. php繁体网站配置,php网页是繁体字怎么办
  3. C# 将PPT转为OFD、DPT、DPS、ODP、POTX、UOP
  4. 大坝安全监测解决方案
  5. Android 批量打包 基于Walle的多渠道快速打包自动脚本
  6. 无公网服务器(ip)做内网穿透
  7. 第十一届蓝桥杯(国赛) 阶乘约数C语言代码
  8. 水滴舆情关于山西体育一周舆情总结
  9. 计算机管理里怎么改ahci,ahci怎么开启_电脑BIOS修改ahci模式教程 - 系统家园
  10. Foxmail中加入Gmail邮箱。对“:ssl连接错误, errorCode:”的修复。