ArthurGray
Newbie
Hello guys, I'm back with a simple question.
I could use some help understanding where's the problem with this piece of code.
Basically it's a method that uses a byte[] password to cycle through a file and xor encrypt it, in a (useless) effort to write bug-free code, I wrote the easiest implementation possible: read a byte, encrypt it, write the result, repeat until no byte is available.
I don't really understand, the output file ends up being even larger than the input!
I checked both files with a binary viewer, the output file is perfectly fine (xor is working) except for the additional bytes, which consist in a large amount of zeros plus some random bytes at the end.. I don't get where do they come from!
I could use some help understanding where's the problem with this piece of code.
Basically it's a method that uses a byte[] password to cycle through a file and xor encrypt it, in a (useless) effort to write bug-free code, I wrote the easiest implementation possible: read a byte, encrypt it, write the result, repeat until no byte is available.
Code:
private void xorFile(String filename, String dest)
{
try {
FileInputStream is = new FileInputStream(filename);
FileOutputStream os = new FileOutputStream(dest);
int b = is.read();
int index = 0;
while(b != -1)
{
int c = ((byte)b ^ password[index % password.length]);
index++;
os.write(c);
b = is.read();
}
os.flush();
os.close();
is.close();
}
catch( Exception e )
{}
}
I checked both files with a binary viewer, the output file is perfectly fine (xor is working) except for the additional bytes, which consist in a large amount of zeros plus some random bytes at the end.. I don't get where do they come from!
Last edited: