Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Encypted file contents to String  RSS feed

 
Joe Wilkinson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a file that has been encrypted (using bouncycastle for RSA encyption) but I now need to read from the file and get a String from it. I have used FileReader and Stringwriter to do so. The problem I have is that the FileReader.read() method is returning 65533 for a certain part of the file. But the StringWriter.write(int c) method seems to be writing with the value of 236 instead of the 65533. I have also tried using a FileInputStream instead of the file reader but to no avail. I suspect the problem is when the StringWriter writes the 65533 it gets converted due to the fact that the 16 high-order bits are ignored by the Writer class. I find this very strange as surely the 16 low order bits results in 65535 ((2^16)-1) which although very close to the 65533 is still higher than it. Is there a different class I could use to get the contents converted to a String? Below is a snippet of my code (using the FileInputStream):
FileInputStream inFile=new FileInputStream(filename);
StringWriter sw = new StringWriter();
int bit=0;
while ((bit = inFile.read())>=0){
    sw.write(bit);
    sw.flush();
}
return sw.toString();
Is it possible that the StringWriter.toString() method is actually the problem here?
Thanks,
Joe
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect you need to use an InputStreamReader to convert the FileInputStream into chars using some particular encoding (which you can specify). I really don't know what encoding is appropriate here - if the file is encrypted, what sort of String representation are you hoping for anyway? Won't it just be a bunch of gibberish? Perhaps this will give you some ideas though...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!