This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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();
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.
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()?
Joined: Feb 08, 2002
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?
Joined: Feb 08, 2002
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();