encryption - Android read file from CipherInputStream without rewrite the file -
is there know how read decrypted file without rewrite original file?
after downloading file, file automatically encrypted. when want open file, file decrypted first problem how read file cipherinputstream no need convert file 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 decrypted file duplicate between encrypted , original if use code above.
fileoutputstream fos = new fileoutputstream(file1 + "/decrypted.json");
writes file object passed in first parameter decrypt method
decrypt(file file1 ...
which file object reading in. when fos.write(d, 0, b);
you writing file object same object reading from.
so either write different file or don't write out @ all. new fileoutputstream
method can take file name instead of file object described here relevant excerpt states
fileoutputstream (string name, boolean append)
creates file output stream write file specified name. if second argument true, bytes written end of file rather beginning. new filedescriptor object created represent file connection.
first, if there security manager, checkwrite method called name argument.
if file exists directory rather regular file, not exist cannot created, or cannot opened other reason filenotfoundexception thrown.
so maybe want fileoutputstream fos = new fileoutputstream("/decrypted.json", true|false)
setting second parameter either true or false depending on whether or not want file appended or overwritten?
it's little difficult provide exact solution problem don't state desired result have made assumptions may wrong either way above should find solution need
Comments
Post a Comment