Reputation: 1156
I have a String which contains 2 byte (16 bit) ASCII characters from encryption. Then I write it to a file with this code:
String result = encrypt("text"); //some encryption method
FileOutputStream fos = new FileOutputStream(filename);
fos.write(result.getBytes("ISO-8859-15"));
fos.flush();
fos.close();
The problem is when I read the file, the string value is already different. Below is the code which I use to read the file:
InputStream inputStream = new FileInputStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "ISO-8859-15"));
String line;
String txt = "";
while ((line = br.readLine()) != null) txt = txt + line;
I found that String txt is already different than String result. I even made a method to sum all the character's ASCII in the string, and found it different. And I have no idea what's my mistake. Please help.
Upvotes: 0
Views: 1496
Reputation: 93559
If you've encrypted it, you don't want to write it out as a string and apply an encoding. Every encryption method I know of treats data as an array of bytes. You need to do any character set converting before encryption, and after decryption. In Java you shouldn't even be holding the post-encryption data as a string, it ought to be an array of bytes.
Also, there is no such thing as 16 bit ASCII. ASCII is 8 bit. You're using some sort of wide character data or 16 bit unicode, not ascii. http://en.wikipedia.org/wiki/ASCII
Upvotes: 1