using fileinputstream to get part of encrypted file

493 views Asked by At

I'm trying to decrypt a large file using https://github.com/martinwithaar/Encryptor4j/blob/master/src/main/java/org/encryptor4j/util/FileEncryptor.java the following is part of decrypt method that i modified

                //the following does not work 
                FileInputStream fis=new FileInputStream(src);
                fis.skip(83);
                is = encryptor.wrapInputStream(fis);
                os = new FileOutputStream(dest);
                //the copy method is a default method from FileEncryptor.java
                copy(is, os);

the following works fine.(i have to decrypt entire file and then read/save part of file to another file then delete the old one and rename the new one to the old file name.

                is = encryptor.wrapInputStream(new FileInputStream(src));
                os = new FileOutputStream(dest);

                copy(is, os);

                FileInputStream fis=new FileInputStream(dest);
                fis.skip(67);
                FileOutputStream fos=new FileOutputStream(dest+".2");
                copy(fis,fos);
                new File(dest).delete();
                new File(dest+".2").renameTo(new File(dest));

                fis.close();
                fos.close();

my question is, why the code on top does not work? I followed http://www.tutorialspoint.com/java/io/fileinputstream_skip.htm on how to skip some bytes.

1

There are 1 answers

4
user207421 On

Because the encrypted stream has state. The encryption of the 84th byte depends on the prior encryption history. Try this:

FileInputStream fis=new FileInputStream(src);
is = encryptor.wrapInputStream(fis);
is.skip(83);
os = new FileOutputStream(dest);
// ...