现在写了个加密程序,AES似乎到了20M之后,就报java.lang.OutOfMemoryError: Java heap space异常        byteIn = new byte[(int) file.length()];
        bis.read(byteIn);
        outFile = FileHelper.renameFileToFixName(outFile, outFile.getName());
     out=new FileOutputStream(outFile);
     bos = new BufferedOutputStream(out);
        Cipher cipher = initCipher(Cipher.ENCRYPT_MODE);
        byteOut = cipher.doFinal(byteIn);
        bos.write(byteOut);我不想要CiperInputStream 这样的类,因为当加密之后,密钥不同解密不报错。
请问如何实现大文件的加密,解密

解决方案 »

  1.   

    一般大文件加密是先使用des将大文件加密,然后用aes将des的秘钥进行加密
      

  2.   

    我做过这个    byteIn = new byte[(int) file.length()];这里是不行的  如果这个文件有5G你直接就内存不足了    所以你需要分段加密     就想复制一个文件一样   从a 复制到 b   只是复制过程中你需要每500kb的复制   并且读取这500kb数据到内存之后调用加密方法对内存中的这500kb字节进行加密  然后再写入到b
      

  3.   

    byteIn = new byte[(int) file.length()];
    bis.read(byteIn);膜拜
    请问读20G的文件会怎么样
      

  4.   


    byte[] buf = new byte[800 * 1024];
    int read;
    long pos = 0;
    bt.start();

    byte[] key=password.getBytes("UTF-8");
    Crypto c=new Crypto(key, key.length);
    byte[] result;
    while ((read = fis.read(buf)) > 0) {
    result = deEncrypt(buf, read, c);
    fos.write(result, 0, result.length);
    pos+=read;
    if (!bt.back(read)) {
    break;
    }
    }
      

  5.   


    int blockSize = cipher.getBlockSize() * 1000;  
            int outputSize = cipher.getOutputSize(blockSize);  
      
            byte[] inBytes = new byte[blockSize];  
            byte[] outBytes = new byte[outputSize];  
      
            int inLength = 0;  
            boolean more = true;  
            while (more) {  
                inLength = in.read(inBytes);  
                if (inLength == blockSize) {  
                    int outLength = cipher.update(inBytes, 0, blockSize, outBytes);  
                    out.write(outBytes, 0, outLength);  
                } else {  
                    more = false;  
                }  
            }  
            if (inLength > 0)  
                outBytes = cipher.doFinal(inBytes, 0, inLength);  
            else  
                outBytes = cipher.doFinal();  
            out.write(outBytes);