【发布时间】:2018-03-03 21:38:22
【问题描述】:
实际上我得到了 InvalidKeyException: Illegal key size,但相同的代码正在生产中工作。当我尝试在本地运行此代码时,在以下行中解码时遇到了密钥大小问题:
cipher.init(2, new SecretKeySpec(secretKey, "AES"), new IvParameterSpec(initVector));
在上面的行中,我得到以下异常:
public byte[] getPageByteStream(String fileName)
throws DMSApplicationException
{
logger.info(GridFsPagesDAOImpl.class + " Entering in to getPageByteStream DAO Method : " + fileName);
Query searchQuery = new Query(Criteria.where("filename").is(fileName));
GridFSDBFile gridFSDBFile = DmsDBUtils.getGridFsOperations().findOne(searchQuery);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte[] results = null;
byte[] initVector = null;
try {
gridFSDBFile.writeTo(stream);
byte[] bytes = null;
bytes = stream.toByteArray();
Base64 base64 = new Base64();
byte[] decodedArr = base64.decode(bytes);
byte[] decArr = Arrays.copyOfRange(decodedArr, 24, bytes.length);
byte[] secretKey = base64.decode("mkJmh3d2WLNXgmWIv4znTU+IXk7XczlInO9mXmv1iBE=\n");
String str = new String(secretKey, "UTF-8");
System.out.println("decodes string : "+str);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
initVector = Arrays.copyOfRange(decodedArr, 8, 24);
cipher.init(2, new SecretKeySpec(secretKey, "AES"), new IvParameterSpec(initVector));
decArr = Arrays.copyOfRange(decodedArr, 24, bytes.length);
byte[] decArr1 = Arrays.copyOfRange(decArr, 0, decArr.length - decArr.length % 16);
results = cipher.doFinal(decArr1);
} catch (Exception e) {
e.printStackTrace();
logger.info(GridFsPagesDAOImpl.class + " Exiting from getPageByteStream DAO Method " + e);
if (gridFSDBFile != null) {
try {
stream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
finally
{
if (gridFSDBFile != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
logger.info(GridFsPagesDAOImpl.class + " Exiting from getPageByteStream DAO Method " + fileName);
return results;
}
}
你能推荐我吗?
帮助非常明显。
【问题讨论】:
-
你可能需要安装无限javacript扩展,见oracle.com/technetwork/java/javase/downloads/…
-
或者只使用与 256 位密钥一样安全的 128 位密钥,因为两者都不能被暴力破解。
-
是的,可能是某些代码有问题,但我的同一段代码在我的生产 jboss 服务器上工作正常,但是当我尝试本地连接以下载文件时,这个异常我得到了。
-
您的生产服务器要么安装了无限强度策略,要么正在使用没有策略限制的 OpenJDK 变体。你需要你的本地系统来做同样的事情。
-
是的 @dave_thompson_085 你是对的。谢谢。
标签: java arrays encryption aes decoding