rizal
rizal

Reputation: 85

Android read file from CipherInputStream without rewrite the file

Is there someone who know how to read decrypted file without rewrite it into the original file?

After downloading the file, the file automatically encrypted. When I want to open the file, the file will be decrypted first but the problem is how to read the file from CipherInputStream so that no need to convert the file back to the original.

void decrypt(File file1, String nama) throws IOException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException {

    md5 hash = new md5();
    String sapi = hash.md5(nama);

    FileInputStream fis = new FileInputStream(file1+ "/" + sapi);

    FileOutputStream fos = new FileOutputStream(file1 + "/decrypted.json");

    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
            "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();

}

I can open the decrypted file but it will duplicate between encrypted and original if I use the code above.

Upvotes: 1

Views: 669

Answers (1)

jamesc
jamesc

Reputation: 12837

FileOutputStream fos = new FileOutputStream(file1 + "/decrypted.json"); 

writes to the File object passed in as the first parameter to the decrypt method

decrypt(File file1 ...

Which is also the file object you are reading in. So when you do fos.write(d, 0, b);you are writing back to that File object which is the same object that you are reading from.

So either write to a different file or just don't write anything out at all. The new FileOutputStreammethod can take a file name instead of a File object as described here Relevant excerpt states

FileOutputStream (String name, boolean append)

Creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.

First, if there is a security manager, its checkWrite method is called with name as its argument.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

so maybe you want FileOutputStream fos = new FileOutputStream("/decrypted.json", true|false)

setting the second parameter to either true or false depending on whether or not you want the file appended to or overwritten?

It's a little difficult to provide an exact solution to your problem as you don't clearly state what your desired result is so I have made some assumptions which may be wrong but either way the above should help you find the solution you need

Upvotes: 1

Related Questions