微信 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.jarUS_export_policy.jar,但是这个默认的只支持 128位的解密(java 版本在 1.8.0_161之后就没有这个问题了,默认是支持)。我们的版本是 1.8.0_151 正好默认是只支持 128位的解密(其实不是不支持,只是默认配置的不支持)。

那么在哪些场景会用到 256位解密的呢?

  1. 微信发送消息(当使用了安全模式)

  2. 微信退款

解决办法

在前面我们没有提及一个东西,就是在/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 三种解决办法的更多相关文章

  1. 微信退款异步通知报错Illegal key size or default parameters 的解决办法

    问题原因: Java几乎各种常用加密算法都能找到对应的实现.因为美国的出口限制,Sun通过权限文件(local_policy.jar.US_export_policy.jar)做了相应限制.因此存在一 ...

  2. 报错 Illegal key size or default parameters

    简介: java中使用AES对称加密后,请求报错: Caused by: java.lang.RuntimeException: java.security.InvalidKeyException: ...

  3. Oracle 12c报错:ORA-01078和LRM-00109的解决办法

    Oracle 12c报错:ORA-01078和LRM-00109的解决办法 2017-12-17 10:25:30 lemon_love1 阅读数 4336  收藏 更多 分类专栏: oracle   ...

  4. java中的AES 256算法遇到 Illegal key size or default parameters错的解决办法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  5. Java企业微信开发_Exception_02_java.security.InvalidKeyException: Illegal key size

    今天换了重新装了一个jdk,然后运行昨天还好好的企业微信工程,结果启动的时候就给我报了这么个错: java.security.InvalidKeyException: Illegal key size ...

  6. Java 解密错误InvalidKeyException: Illegal key size解决方法

    做解密操作,出现如下错误 java.security.InvalidKeyException: Illegal key size // 设置解密模式为AES的CBC模式 Cipher cipher = ...

  7. yum本地安装rrdtool报错,提示版本冲突的解决办法

    [root@mysql-mon40 mm]# yum localinstall rrdtool-1.4.7-1.el6.rfx.x86_64.rpm -y 错误: Multilib version p ...

  8. sql注入 报错注入常用的三种函数

    1.floor()函数 报错原因是 报错的原因是因为rand()函数在查询的时候会执行一次,插入的时候还会执行一次.这就是整个语句报错的关键 前面说过floor(rand(0)*2) 前六位是0110 ...

  9. PHP安装mcrypt.so报错 mcrypt.h not found 的解决办法

    报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了很多,包括自带的 yum install libm ...

随机推荐

  1. SVN工具常用功能总结

    使用SVN作为版本管理工具,可以使用VisualSVN Server+TortoiseSVN搭建SVN版本控制系统,组长安装VisualSVN Server,组员安装TortoiseSVN. Tort ...

  2. 3、Struts2的API访问和数据封装

    一.API的访问 在使用Struts2的框架的过程中,发现Struts2和Servlet的API是解耦合的.在实际开发中,经常使用到Servlet的API,比如进行登录,将用户的信息保存到Sessio ...

  3. python3.8安装flask出现错误“ModuleNotFoundError: No module named '_ctypes'”

    本想在CentOS下配置flask+nginx+uwsgi环境,结果安装最基础的flask包都出了问题...以下是我的环境: 服务器:阿里云ECS CentOS7 python版本:3.8.0 问题描 ...

  4. unity 基于scrollRect实现翻页显示

    unity 基于scrollRect实现翻页显示,并定为到某一页,而不是某一页的中间方法(第二个脚本采用实际位置计算,并在update里实现平滑过渡): 组场景时,经常需要获取鼠标(或者点击)开始结束 ...

  5. SpringBoot系列之YAML配置用法

    1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...

  6. dig-基本使用

    dig:Domain Information Groper,是一个DNS查询工具 1:使用google的域名服务器:查询特定域名的A记录 [root@localhost ~]# dig @8.8.8. ...

  7. 读effection java

    1.考虑用静态工厂方法代替构造器 public static Boolean valueOf(boolean b){ return b?Boolean.TRUE:Boolean.FALSE; } 静态 ...

  8. vue---Excel表格导出

    一.安装依赖 npm install file-saver --save npm install xlsx --save npm install script-loader --save-dev 二. ...

  9. 前端技术之:如何创建一个NodeJs命令行交互项目

    方法一:通过原生的NodeJs API,方法如下:   #!/usr/bin/env node # test.js var argv = process.argv; console.log(argv) ...

  10. 在虚拟机上的关于FTP FTP访问模式(本地用户模式)

    首先你要有vsftpd服务 可以先去yum中下载(当然你要有本地yum仓库) 输入命令: yum  install  vsftpd 下载完成之后打开vsftpd服务 输入命令:systemctl   ...