微信 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. springboot依赖的一些配置:spring-boot-dependencies、spring-boot-starter-parent、io.spring.platform

    springboot里会引入很多springboot starter依赖,这些依赖的版本号统一管理,springboot有几种方案可以选择. 一.spring-boot-dependencies 有两 ...

  2. Flask的使用以及返回值(其中Response后续详细单独补充)

    一.使用 安装依赖pip3 install flask 第一步 创建Flask对象 from flask import Flask app =Flask(__name__) 第二步 创建路由 @app ...

  3. Python_文本的读写操作

    [需求] 1. 获取文本内容,提取内容中的可用信息,对信息进行清洗等一系列处理 2. 算法输出一些内容,保存到文本文件中,便于使用 [函数] 在Python中open()函数是用来打开文件的,包括文本 ...

  4. 代码审计-phpcms9.6.2任意文件下载漏洞

    漏洞文件: phpcms\modules\content\down.php 1.在download函数中对文件的校验部分 首先 if(preg_match('/(php|phtml|php3|php4 ...

  5. [POJ2262] Goldbach’s Conjecture

    Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48161   Accepted: ...

  6. Grid实现table表格布局

    HTML <div class="table"> <div class="th"> <div>日期</div> ...

  7. 简易数据分析 13 | Web Scraper 抓取二级页面

    这是简易数据分析系列的第 13 篇文章. 不知不觉,web scraper 系列教程我已经写了 10 篇了,这 10 篇内容,基本上覆盖了 Web Scraper 大部分功能.今天的内容算这个系列的最 ...

  8. Class constructor FileManager cannot be invoked without 'new'

    bug:今天项目重新安装依赖打包的时候出现错误:Class constructor FileManager cannot be invoked without 'new' 解决:尝试了很多种解决方案, ...

  9. Python字典及相关操作(内含例题)

    Python字典类型 今天将会介绍一种在python中十分常见的组合数据类型——字典 通过一些实例来理解字典中的常规操作 什么是字典类型? 列表中查找是通过整数的索引(元素在列表中的序号)来实现查找功 ...

  10. Unity调用Android

    1.Unity调用Android 根据unity用户手册,unity可以采用native code(c/c++),但是也提供了C#调用方法,本文对此进行简单讲解. 2.Unity获取android类 ...