尝试理解在标记重复之前

尝试理解在标记重复之前

本文介绍了尝试理解在标记重复之前:InvalidKeyException:非法的密钥大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

Actually I am getting InvalidKeyException: Illegal key size, but same code is working in production. When I am trying to run this code locally, I am facing key size issue while decoding in below line:

 cipher.init(2, new SecretKeySpec(secretKey, "AES"), new IvParameterSpec(initVector));

在上面一行我得到以下异常:

In above line I am getting following exception:

 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;
  }
}

你能建议我吗?

帮助非常明显。

推荐答案

看起来你正在使用AES 256位密钥。

It looks like you're using AES with a 256 bit key.

您需要安装Java Cryptography Extension(JCE)Unlimited Strength Jurisdiction Policy文件才能使用大于128位的密钥。

You need to install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy files to use keys larger than 128 bits.

这篇关于尝试理解在标记重复之前:InvalidKeyException:非法的密钥大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:24