简体   繁体   中英

BufferedReader.readLine() mangles binary

For academic purposes, I am working on a small encryption-based application using java and java crypto. That said, I generally work in C or C++ (where this happens to be very easy) so I apologize if this is an obvious issue. So my issue comes when I want to decrypt information. The problem is not so much related to using crypto or anything, but, in fact, retrieving my initialization vector properly. For now I have a plaintext file with the following format new-line/colon delimited (yes, this is insecure but it's an academic exercise - obviously the key file would not be in plaintext in the real world)

password:salt:<IV>

Where <IV> is the initialization vector. I decided to write out the initialization vector directly in binary format. Now I here is where my problem begins. I can write out the data properly (verified using hexdump), but it does not seem that it is being properly read back into the program. For instance, consider a counter value of

00 18 C0 98 (decimal: 1622168)

This value is correctly written out. However, when I try to read it in, this value is all wrong. I suspect this is a result of BufferedReader.readLine() . I simply read this line and write it out to another file and this value is then changed to

BD EF BF BD (decimal: who cares, very wrong...)

But the ASCII values (password and salt) remain intact as well as the colon delimiters. The initialization vector binary is all the gets scrambled.

What I am trying to do is this:

BufferedReader reader = new BufferedReader(new FileReader(f));
String last  = null;
String parse = null;
while((last = reader.readLine()) != null) {
    parse = last;
}
reader.close();
int offset = // A few indexOf() statements to find the positon;
byte[] iv  = parse.substring(offset).getBytes();

At this point, iv.length > 16 (using 128-bit AES) which is already wrong. So I am not entirely sure how I can accomplish this ASCII-binary mix (perhaps it is bad design to begin with). Part of my issue is that char != byte in Java and I cannot simply read everything in as byte's if I want to read a full line (using readLine() anyway). But is there anyway to get around this issue? Since I want the full line, but I also want to extract binary from this line.

Additionally, I have also tried a direct conversion

for(int i = 0 ; i < 16 ; ++i)
  iv[i] = (byte)parse.charAt(offset + i);

But this does not work either. The other code ( getBytes() -based) was working on my personal machine with java version "1.6.0_37" , but the deployment environment is using java version "1.6.0_33" where this is failing. Any help is appreciated.

What is a line of binary data? How do you know which 0x0A is data and which is a newline?

The point is BufferedReader is meant for reading text , not arbitrary data! It will treat all of your data as text. It will also attempt to convert your binary to text using some encoding, which will of course mangle your data.

A better method would be to either:

  • Not write any of your data as binary, and rely on newlines or some other delimiter. (eg keep using BufferedReader.readLine() )
  • Write all of your data as binary, and either make each record in your file of a fixed length, or come up with some more complicated delimiter scheme. (eg just use FileInputStream.read(byte[]) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM