尝试理解在标记重复之前:InvalidKeyException: Illegal key size
Try to understand Before marking duplicate: InvalidKeyException: Illegal key size
实际上我收到了 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;
}
}
你能推荐我吗?
非常感谢您的帮助。
您似乎在使用带有 256 位密钥的 AES。
您需要安装 Java 加密扩展 (JCE) 无限强度管辖策略文件才能使用大于 128 位的密钥。
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
实际上我收到了 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;
}
}
你能推荐我吗?
非常感谢您的帮助。
您似乎在使用带有 256 位密钥的 AES。
您需要安装 Java 加密扩展 (JCE) 无限强度管辖策略文件才能使用大于 128 位的密钥。
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html