解决:java.security.NoSuchAlgorithmException:Cannot find any provider supporting AES/CBC/PKCS7Padding小记

由于小程序开发的需要,微信接口返回的敏感信息的加密数据需要在后台进行解密,以供开发使用。但是,在解密过程中会发生以下异常:

[En]

Due to the needs of Mini Program development, the encrypted data of sensitive information returned by Wechat API needs to be decrypted in the background for development use. However, the following exception occurs during decryption:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
1
在线搜索的原因是,Java原生JDK不支持PKCS7Pending填充,解决方案很多。例如,有人说,PKCS5填充和PKCS7填充填充方法差别不大,所以都可以使用。

[En]

The reason for online search is that java native jdk does not support PKCS7Padding filling, and there are many solutions. For example, some people say that there is little difference between PKCS5Padding and PKCS7Padding filling methods, so they can all be used.

遗憾的是,它对我不起作用。

[En]

It’s a pity that it didn’t work for me.

所以我继续在知识的海洋中漂浮!

[En]

So I continue to bei (rou) and float (lin) in the ocean of knowledge!

我很幸运能找到博主的一篇博文,但如果我知识不够渊博,我仍然没有解决我的问题,但我感觉我离真相很近了,加油!

[En]

I am very lucky to find a blog post from a blogger, but if I am not knowledgeable enough, I still have not solved my problem, but I feel that I am close to the truth, come on!

所以我继续在知识的海洋中漂浮!

[En]

So I continue to bei (rou) and float (lin) in the ocean of knowledge!

<<<<<<<<<<<<<<<<<<

最后,在我不屑的努力下,找到了明确、小白的至高无上的指导,请大家阅读我的博客,或者看看我的下一篇(~-^-)~

[En]

Finally, in my disdain efforts, found a clear, Xiaobai supreme guidance, please read the blog, or see my next (~-^ -) ~

解决方式:
在 jdk安装目录中(%JAVA_HOME%\jre\lib\ext)添加 jar 包 bcprov-jdk16-1.46.jar。(我添加的是这个,下载的最新的,下载地址来自上面那篇博文,版本与原博文不太一样,但亲测可用)

在 jdk安装目录下( %JAVA_HOME%\jre\lib\security )修改 java.security 文件,将第74行(我的是在74行)的

security.provider.7=com.sun.security.sasl.Provider 替换为 security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider

注意以下几点,记住要备份这个文件,如果崩溃,它将是JJ

[En]

Note the following, remember to back up this file, in case of collapse, it will be jj

然后,运行项目,确定,并成功解密它。

[En]

Then, run the project, OK, and successfully decrypt it.

还有一个需要注意的领域:

[En]

There is another area to pay attention to:

Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS7Padding”,”BC”);
其中的 “BC”需要加上,不然会报异常:

javax.crypto.BadPaddingException: pad block corrupted
原因:因为BC是一个provider,而org.bouncycastle.jce.provider.BouncyCastleProvider是个第三方的库。如果jce自带的就可以不用加BC,但是JCE不支持PKCS7Padding的填充方式。

借鉴博文:
https://www.cnblogs.com/chen-lhx/p/6233954.html
https://my.oschina.net/u/269777/blog/1801356
https://blog.csdn.net/yuanhangLVli/article/details/82152178

[En]

转自: https://blog.csdn.net/qq_43225978/article/details/94459412

Original: https://www.cnblogs.com/austinspark-jessylu/p/15384550.html
Author: 爷的眼睛闪亮
Title: 解决:java.security.NoSuchAlgorithmException:Cannot find any provider supporting AES/CBC/PKCS7Padding小记

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/7221/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总