首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解密文件

解密文件
EN

Stack Overflow用户
提问于 2013-09-06 07:08:15
回答 1查看 1.5K关注 0票数 3

因此,我有这些大文件(6GB+),我需要在32位计算机上解密。我以前使用的一般过程是在内存中读取整个文件,然后将其传递给解密函数,然后将其全部写回一个文件。由于内存限制,这实际上不起作用。我确实尝试过将文件分部分传递给解密函数,但在将文件发送到解密函数之前,它似乎在拆分文件的边界上出现了问题。

我试过将文件按键大小分成几个部分,但这似乎并不重要。我尝试了一个大小为2048的字节数组,以及一个大小为294的字节数组,认为这可能是一个特殊的边界,但没有运气。我可以看到文件的部分正确解密,但部分完全是胡说八道。

难道不可能对文件进行块解密吗?如果有办法,那怎么做?

这是我的解密函数/我的部分解密尝试。

代码语言:javascript
复制
  private Path outFile;

  private void decryptFile(FileInputStream fis, byte[] initVector, byte[] aesKey, long used) {
    //Assume used = 0 for this function. 
    byte[] chunks = new byte[2048]; //If this number is greater than or equal to the size of the file then we are good.
    try {
      if (outFile.toFile().exists())
        outFile.toFile().delete();
      outFile.toFile().createNewFile();
      FileOutputStream fos = new FileOutputStream(outFile.toFile());
      OutputStreamWriter out = new OutputStreamWriter(fos);
      IvParameterSpec spec = new IvParameterSpec(Arrays.copyOfRange(initVector, 0, 16));
      SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
      Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
      cipher.init(Cipher.DECRYPT_MODE, key, spec);
      int x;
      while ((x = fis.read(chunks, 0, chunks.length)) != -1) {
        byte[] dec = cipher.doFinal(Arrays.copyOfRange(chunks, 0, x));
        out.append(new String(dec));
      }
      out.close();
      fos.close();
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(ExceptionUtils.getStackTrace(e));
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-06 07:39:45

考虑在多部分操作中使用Cipher#update(byte[],int,int,byte[],int)而不是doFinal()。这会为你解决部分边界问题。

通过调用doFinal(byte[] output, int outputOffset)方法可以获得解密数据的最后一部分。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18651907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档