Illegal key size
异常:java.security.InvalidKeyException: Illegal key size
解决:密钥长度修改为128位或者使用JCE无限制权限策略文件
原因:如果密钥大于128位,会抛出java.security.InvalidKeyException: Illegal key size
异常。因为密钥长度是受限制的,java运行时环境读到的是受限的policy文件。文件位于${java_home}/jre/lib/security
,这种限制是因为美国对软件出口的控制。
替换:去官方下载JCE无限制权限策略文件,下载地址 JDK 5、JDK 6、JDK 7、JDK 8。
下载后解压,可以看到local_policy.jar
和US_export_policy.jar
以及readme.txt
如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security
目录下覆盖原来的文件
如果安装了JDK,还要将两个jar文件也放到%JDK_HOME%\jre\lib\security
目录下覆盖原来文件。
参考:这里
Given final block not properly padded
异常:javax.crypto.BadPaddingException: Given final block not properly padded
错误代码:
private Key initKeyForAES(String key) throws NoSuchAlgorithmException {
if (null == key || key.length() == 0) {
throw new NullPointerException("key not is null");
}
SecretKeySpec key2 = null;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(key.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
key2 = new SecretKeySpec(enCodeFormat, "AES");
} catch (NoSuchAlgorithmException ex) {
throw new NoSuchAlgorithmException();
}
return key2;
}
问题修复:
private Key initKeyForAES(String key) throws NoSuchAlgorithmException {
if (null == key || key.length() == 0) {
throw new NullPointerException("key not is null");
}
SecretKeySpec key2 = null;
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
key2 = new SecretKeySpec(enCodeFormat, "AES");
} catch (NoSuchAlgorithmException ex) {
throw new NoSuchAlgorithmException();
}
return key2;
}
解决:
即,将
new SecureRandom(key.getBytes())
替换为
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
参考:这里
Stream closed before a reply was received
分析发现是客户防火墙配置导致的网络时断时续的原因
java.io.IOException: Stream closed before a reply was received
at com.turo.pushy.apns.ApnsClientHandler.<clinit>(ApnsClientHandler.java:80)
at com.turo.pushy.apns.ApnsClientHandler$ApnsClientHandlerBuilder.build(ApnsClientHandler.java:136)
at com.turo.pushy.apns.ApnsClientHandler$ApnsClientHandlerBuilder.build(ApnsClientHandler.java:89)