KVK
KVK

Reputation: 1289

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

can any one help me to fix this?

my code:

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

Upvotes: 2

Views: 1234

Answers (2)

Linus
Linus

Reputation: 894

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. This should be easy because you're already using the stream methods.
  2. Use a ByteArrayOutputStream instead of a FileOutputStream when decrypting.
  3. If you need to re-encrypt the data when your finished you'd use a ByteArrayInputStream to encrypt.

The only drawback I see is that getting the buffer from the ByteArrayOutputStream is apparently a copy operation.

Upvotes: 1

user207421
user207421

Reputation: 310883

What you're looking for is javax.crypto.CipherInputStream.

Upvotes: 1

Related Questions