Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

load Properites from ByteArrayInputStream

 
Cindy Cindy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to decrypt an encrypted property file and then load it. The file is decrypted using a CipherInputStream and read into a byte array. I'm then attempting to use a ByteArrayInputStream to load the properties file. When the load method tries to read a line, it returns null. If I write the byte array to a FileOutputStream, read it into a FileInputStream, and load it, it works fine. However, that defeats the purpose of encrypting the file in the first place.
Why can't I load a ByteArrayInputStream? How can I convert the byte array into the appropriate format so I can load the properties?
FileInputStream fisCipher = new FileInputStream("test.props");
byte[] decryptedBytes = Cryptography.decryptInfo(fisCipher);
fisCipher.close();
ByteArrayInputStream bis = new ByteArrayInputStream(decryptedBytes);
props.load(bis);
bis.close();
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dunno. I'd try writing the byte array to a file as an intermediate step, so you can look at the file and see if it looks OK. Also you can print out the bytes, as characters and as numbers, as an aid in verifying they're correct:

If you don't see a valid-looking properties file, then you'll want to focus on debugging the Cryptography class.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cindy G:

byte[] decryptedBytes = Cryptography.decryptInfo(fisCipher);
fisCipher.close();


I'm not familiar with the Cryptography class...is that a custom class? You're calling a static method decryptInfo?
What exactly is it returning? Is it returning a new byte[] instance, or is it returning the buffer internal to the Cryptography class? If it's returning the internal buffer, won't that get clobbered when you call fisCipher.close()?
 
Cindy Cindy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In response to the two previous posts...
I'm using the Java Cryptology Extension and am using the CipherOutputStream in ENCRYPT_MODE to write an encrypted properties file. I'm then calling my own static method, decryptInfo() which uses a CipherInputStream in DECRYPT_MODE to decrypt the file. I'm using the read(byte[]) method in CipherInputStream which decrypts the file into a byte array. Then I'm trying to use a ByteArrayInputStream to load (Properties.load()) that byte array. The Properties.load() method returns null when it tries to read the first line.
I have tried to write the byte array to a file and read it in again. The format is fine and I can then load the properties. However, writing the decrypted contents to a file to read it back in somewhat defeats the purpose of encrypting it in the first place.
Any ideas?
 
Cindy Cindy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is fixed (thanks Rob). I was closing the file too early. I thought it was OK since I still had data in the byte array and could later right the byte array to a file. Apparently not.
FileInputStream fisCipher = new FileInputStream("test.props");
byte[] decryptedBytes = Cryptography.decryptInfo(fisCipher);
ByteArrayInputStream bis = new ByteArrayInputStream(decryptedBytes);
props.load(bis);
bis.close();
fisCipher.close();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic