微信 AES 解密报错 Illegal key size 三种解决办法
微信 AES 解密报错 Illegal key size
Java 环境
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
问题
问题日志
最近在迁移的服务器,在迁移完之后, 一个有关微信小程序的日志打印下面的报错信息。
c.t.b.a.c.weixin.aes.WXBizMsgCrypt - 小程序解密异常
java.security.InvalidKeyException: Illegal key size
解密失败,看了下解密的密钥是正确的,没有任何问题。 这个在 经典 下是可以运行的,在 VPC 下运行不了。 (因为最近在进行阿里云网络迁移)
问题原因
微信在进行数据传输的时候,会进行加密,微信使用的 AES 加密使用的是 256位,Java 默认使用的解密包是 local_policy.jar 和 US_export_policy.jar,但是这个默认的只支持 128位的解密(java 版本在 1.8.0_161之后就没有这个问题了,默认是支持)。我们的版本是 1.8.0_151 正好默认是只支持 128位的解密(其实不是不支持,只是默认配置的不支持)。
那么在哪些场景会用到 256位解密的呢?
- 微信发送消息(当使用了安全模式)

- 微信退款

解决办法
在前面我们没有提及一个东西,就是在/usr/local/java/jdk1.8.0_151/jre/lib/security/policy/下有两个目录。
[root@djx-117106 policy]# pwd
/usr/local/java/jdk1.8.0_151/jre/lib/security/policy/
[root@djx-117106 policy]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Nov 2 10:47 limited
drwxr-xr-x 2 root root 4096 Nov 2 10:47 unlimited
[root@djx-117106 policy]# ls -l ./limited/
total 8
-rw-r--r-- 1 root root 3405 Jul 4 19:41 local_policy.jar
-rw-r--r-- 1 root root 2920 Jul 4 19:41 US_export_policy.jar
[root@djx-117106 policy]# ls -l ./unlimited/
total 8
-rw-r--r-- 1 root root 2929 Jul 4 19:41 local_policy.jar
-rw-r--r-- 1 root root 2917 Jul 4 19:41 US_export_policy.jar
有一个 limited 目录(也就是对解密有限制的包,只支持 128位),也有一个 ulimited 目录(也就是没有限制的目录)。
更改 源码
我们在 /usr/local/java/jdk1.8.0_151/jre/lib/security/ 下的 java.security文件中看到。
# To support older JDK Update releases, the crypto.policy property
# is not defined by default. When the property is not defined, an
# update release binary aware of the new property will use the following
# logic to decide what crypto policy files get used :
#
# * If the US_export_policy.jar and local_policy.jar files are located
# in the (legacy) <java-home>/lib/security directory, then the rules
# embedded in those jar files will be used. This helps preserve compatibility
# for users upgrading from an older installation.
#
# * If crypto.policy is not defined and no such jar files are present in
# the legacy locations, then the JDK will use the limited settings
# (equivalent to crypto.policy=limited)
#
# Please see the JCA documentation for additional information on these
# files and formats.
#crypto.policy=unlimited
注意下文中的 (equivalent to crypto.policy=limited) 说明默认是使用的 limited.
我们只需要加 crypto.policy=unlimited. 让默认使用的不限制的。
替换Jar包
替换 /usr/local/java/jdk1.8.0_151/jre/lib/security/policy/limited的路径的包。其实我们可以直接用 /usr/local/java/jdk1.8.0_151/jre/lib/security/policy/unlimited下面的包直接替换 /usr/local/java/jdk1.8.0_151/jre/lib/security/policy/limited/ 下面的两个包。也就是让默认使用不限制的jar包。
升级 Java 版本
https://www.oracle.com/technetwork/java/javase/8u161-relnotes-4021379.html
在官方文档写到,
security-libs/javax.crypto
Unlimited cryptography enabled by default
The JDK uses the Java Cryptography Extension (JCE) Jurisdiction Policy files to configure cryptographic algorithm restrictions. Previously, the Policy files in the JDK placed limits on various algorithms. This release ships with both the limited and unlimited jurisdiction policy files, with unlimited being the default. The behavior can be controlled via the new 'crypto.policy' Security property found in the /lib/java.security file. Please refer to that file for more information on this property.
也就是从 1.8.0_161-b12 版本后,默认将采用无限制的加密算法,也就是使用 unlimited 下的jar包。我们也可以通过 设置 java.security 文件的 crypto.policy的值来改变这个默认的值。
微信 AES 解密报错 Illegal key size 三种解决办法的更多相关文章
- 微信退款异步通知报错Illegal key size or default parameters 的解决办法
问题原因: Java几乎各种常用加密算法都能找到对应的实现.因为美国的出口限制,Sun通过权限文件(local_policy.jar.US_export_policy.jar)做了相应限制.因此存在一 ...
- 报错 Illegal key size or default parameters
简介: java中使用AES对称加密后,请求报错: Caused by: java.lang.RuntimeException: java.security.InvalidKeyException: ...
- Oracle 12c报错:ORA-01078和LRM-00109的解决办法
Oracle 12c报错:ORA-01078和LRM-00109的解决办法 2017-12-17 10:25:30 lemon_love1 阅读数 4336 收藏 更多 分类专栏: oracle ...
- java中的AES 256算法遇到 Illegal key size or default parameters错的解决办法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- Java企业微信开发_Exception_02_java.security.InvalidKeyException: Illegal key size
今天换了重新装了一个jdk,然后运行昨天还好好的企业微信工程,结果启动的时候就给我报了这么个错: java.security.InvalidKeyException: Illegal key size ...
- Java 解密错误InvalidKeyException: Illegal key size解决方法
做解密操作,出现如下错误 java.security.InvalidKeyException: Illegal key size // 设置解密模式为AES的CBC模式 Cipher cipher = ...
- yum本地安装rrdtool报错,提示版本冲突的解决办法
[root@mysql-mon40 mm]# yum localinstall rrdtool-1.4.7-1.el6.rfx.x86_64.rpm -y 错误: Multilib version p ...
- sql注入 报错注入常用的三种函数
1.floor()函数 报错原因是 报错的原因是因为rand()函数在查询的时候会执行一次,插入的时候还会执行一次.这就是整个语句报错的关键 前面说过floor(rand(0)*2) 前六位是0110 ...
- PHP安装mcrypt.so报错 mcrypt.h not found 的解决办法
报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了很多,包括自带的 yum install libm ...
随机推荐
- 程序员IT狗有什么副业可以做呢?
1. 开篇 副业有很多,全网有做什么公众号.闲鱼.手机卡,各种各样的都有,大部分是骗子,小部分是通过自己的努力,获得了成功. 从年初就开始实践如何做一个自由职业者,近大半年有一些感受正好一起分享交流一 ...
- Bran的内核开发教程(bkerndev)-05 打印到屏幕
打印到屏幕 现在, 我们需要尝试打印到屏幕上.为此, 我们需要管理屏幕滚动, 如果能允许使用不同的颜色就更好了.好在VGA视频卡为我们提供了一片内存空间, 允许同时写入属性字节和字符字节对, 可以 ...
- 渗透测试中Linux下反弹shell的认识
最近老是觉得自己白天工作累,晚上理应休息 堕落了几天才发觉自己真垃圾,加紧向前吧. 0x00 前言 在平时渗透还是工作中经常会遇到shell反弹的情况,网上一搜反弹shell都是一大堆,但是真正遇到反 ...
- [POI2015]PIE
题目描述 一张n*m的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色.你有一个a*b的印章,有些格子是凸起(会沾上墨水)的.你需要判断能否用这个印章印出纸上的图案.印的过程中需要满足以下要求:( ...
- 关于_GNU_SOURCE宏
是在features.h中用于特性控制的一个功能测试宏 /user/include/features.h /* If _GNU_SOURCE was defined by the user, turn ...
- Windows突破远程连接最大数去掉限制登录
当对方设置最大连接数 超过限制时 可以用这个命令 win+r 输入 mstsc /v:192.168.18.131:3389 /console windows server 2003 sp2 以 ...
- Springboot项目的jar包目录结构
上图为一个由Luyten工具反编译后的一个jar包 说明: SpringBoot提供的bootstrap的类是放到包的最外面,比如上面的org.springframework.boot.loader. ...
- ESP8266开发之旅 网络篇⑤ Scan WiFi——ESP8266WiFiScan库的使用
1. 前言 现在,通常,为了让手机连上一个WiFi热点,基本上都是打开手机设置里面的WiFi设置功能,然后会看到里面有个WiFi热点列表,然后选择你要的连接上. 基本上你只要打开手机连接WiF ...
- 【redis】redis的过期策略
redis过期策略 在使用redis做缓存的时候,我们常常会设置过期时间.那么redis是如何清理这些过期的数据呢? 答案是: 定期删除 + 惰性删除 定期删除: redis每100ms就会随机抽查删 ...
- Java源码 HashMap.roundUpToPowerOf2原理
int rounded = number >= MAXIMUM_CAPACITY ? MAXIMUM_CAPACITY : (rounded = Integer.highestOneBit(nu ...