简体   繁体   English

在Java中使用加密文件而不解密到磁盘

[英]Using Encrypted Files Without Decrypting To Disk in java

I need to know how can i use the encrypted file without decrypting into the disk.In my application i handle with many images and videos so i need to encrypt those files and use it with out storing decrypted file in the disk. 我需要知道如何在不解密到磁盘的情况下使用加密文件。在我的应用程序中,我可以处理许多图像和视频,因此我需要对这些文件进行加密并在不将解密文件存储在磁盘中的情况下使用。

i am using java do develop my application 我正在使用Java来开发我的应用程序

can any one help me to fix this? 谁能帮我解决这个问题?

my code: 我的代码:

CryptoUtilsTest.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 CryptoException.java

public class CryptoException extends Exception {

    public CryptoException() {
    }

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

CryptoUtils.java 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);
        }
    }
}

您正在寻找的是javax.crypto.CipherInputStream

Ultimately your program needs to allocate and fill the memory itself in some way. 最终,您的程序需要以某种方式分配和填充内存本身。 You could devise separate utilities to do this, but I think the following suggestion may be more flexible: 您可以设计单独的实用程序来执行此操作,但是我认为以下建议可能更灵活:

  1. Change your core doCrypto() to use InputStream and OutputStream instead of File . 将您的核心doCrypto()更改为使用InputStreamOutputStream而不是File This should be easy because you're already using the stream methods. 这应该很容易,因为您已经在使用流方法。
  2. Use a ByteArrayOutputStream instead of a FileOutputStream when decrypting. 解密时,请使用ByteArrayOutputStream而不是FileOutputStream
  3. If you need to re-encrypt the data when your finished you'd use a ByteArrayInputStream to encrypt. 如果需要在完成后重新加密数据,则可以使用ByteArrayInputStream进行加密。

The only drawback I see is that getting the buffer from the ByteArrayOutputStream is apparently a copy operation. 我看到的唯一缺点是,从ByteArrayOutputStream获取缓冲区显然是复制操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM