Java 在标记duplicate:InvalidKeyException:非法密钥大小之前,请尝试理解

Java 在标记duplicate:InvalidKeyException:非法密钥大小之前,请尝试理解,java,arrays,encryption,aes,decoding,Java,Arrays,Encryption,Aes,Decoding,事实上,我得到了InvalidKeyException:非法的密钥大小,但相同的代码正在生产中工作。当我试图在本地运行此代码时,我在下面一行中解码时遇到密钥大小问题: cipher.init(2, new SecretKeySpec(secretKey, "AES"), new IvParameterSpec(initVector)); 在上一行中,我得到了以下例外情况: public byte[] getPageByteStream(String fileName) throws

事实上,我得到了InvalidKeyException:非法的密钥大小,但相同的代码正在生产中工作。当我试图在本地运行此代码时,我在下面一行中解码时遇到密钥大小问题:

 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位的密钥


您可能需要安装无限制的JavaScript扩展,请参阅或仅使用与256位密钥一样安全的128位密钥,因为这两个密钥都无法强制执行。是的,可能某些代码有问题,但我的同一段代码在我的生产jboss服务器上运行良好,但当我尝试在本地连接以下载文件时,我遇到此异常。您的生产服务器已安装无限强度策略,或正在使用不受策略限制的OpenJDK变体。你需要你的本地系统来做同样的事情。是的@dave_thompson_085你完全正确。谢谢。JCE是JVM的一部分,您实际上指的是JCE的无限强度加密策略文件。@MarkrotVeel您说得对。更新。感谢您宝贵的回复。我们怎么能说它是256位密钥。是的,可能是一些代码有问题,但我的同一段代码在我的生产jboss服务器上运行良好,但当我尝试本地连接以下载文件时,我得到了此异常。PS:该URL仅适用于j8,在这一点上是可能的,但不确定。其他Sun/Oracle Java版本可在now Oracle网站的其他位置获得。