在java中使用加密文件而无需解密到磁盘

时间:2015-11-13 06:25:40

标签: java encryption

我需要知道如何在不解密到磁盘的情况下使用加密文件。在我的应用程序中,我处理了许多图像和视频,因此我需要加密这些文件并将其用于将解密文件存储在磁盘中。 / p>

我正在使用java开发我的应用程序

任何人都可以帮我解决这个问题吗?

我的代码:

CryptoUtilsTest.java

import java.io.File;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class CryptoUtilsTest {
    public static void main(String[] args) throws IOException {
        String key = "Mary has one cat";
        File inputFile = new File("/home/anand/Desktop/inputfile.jsp");
        File encryptedFile = new File("/home/anand/Desktop/document.encrypted");
        File decryptedFile = new File("/home/anand/Desktop/.document.decrypted");
      //  PipedInputStream pis = new PipedInputStream();
        //PipedOutputStream pos = new PipedOutputStream(pis);
        try {
            CryptoUtils.encrypt(key, inputFile, encryptedFile);
            CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
        } catch (CryptoException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }
}

CryptoException.java

public class CryptoException extends Exception {

    public CryptoException() {
    }

    public CryptoException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

CryptoUtils.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * A utility class that encrypts or decrypts a file.
 * @author www.codejava.net
 *
 */
public class CryptoUtils {
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";

    public static void encrypt(String key, File inputFile, File outputFile)
            throws CryptoException {
        doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
    }

    public static void decrypt(String key, File inputFile, File outputFile)
            throws CryptoException {
        doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
    }

    private static void doCrypto(int cipherMode, String key, File inputFile,
            File outputFile) throws CryptoException {
        try {
            Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(cipherMode, secretKey);

            FileInputStream inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);

            byte[] outputBytes = cipher.doFinal(inputBytes);

            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);

            inputStream.close();
            outputStream.close();

        } catch (NoSuchPaddingException | NoSuchAlgorithmException
                | InvalidKeyException | BadPaddingException
                | IllegalBlockSizeException | IOException ex) {
            throw new CryptoException("Error encrypting/decrypting file", ex);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您要找的是javax.crypto.CipherInputStream

答案 1 :(得分:1)

最终,您的程序需要以某种方式分配和填充内存。您可以设计单独的实用程序来执行此操作,但我认为以下建议可能更灵活:

  1. 将您的核心doCrypto()更改为使用InputStreamOutputStream而不是File。这应该很简单,因为您已经在使用流方法。
  2. 解密时使用ByteArrayOutputStream代替FileOutputStream
  3. 如果您需要在完成后重新加密数据,请使用ByteArrayInputStream进行加密。
  4. 我看到的唯一缺点是从ByteArrayOutputStream获取缓冲区显然是一个复制操作。