AES加密长度问题

发现问题:

在项目中,有个监测数据传输问题,需要用到AES算法加密,拿到开发环境原始秘钥长度为16字母位,但是部署到服务器上的接收端的密码长度为32字母位。

修改开发环境加密秘钥为32字母位后,报错如下:

 

问题原因

JAVA默认支持AES 128 Bit 的key, 如果你使用 256 Bit key, java complier 会抛出 Illegal key size Exception。Java 默认使用的解密包是 local_policy.jar 和 US_export_policy.jar,但是这个默认的只支持 128位的解密(java 版本在 1.8.0_161之后就没有这个问题了,默认是支持)。我的版本是 1.8.0_151 正好默认是只支持 128位的解密。

解决办法1

/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包。

解决办法2

升级 Java 版本

Java™ SE Development Kit 8, Update 161
在官方文档写到:

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的值来改变这个默认的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值