/    Sign up×
Community /Pin to ProfileBookmark

Problem with CipherInputStream

I’m writing a little utility that will allow our config files to be encrypted, to keep curious developers from mucking around with settings they shouldn’t touch. My encrypt method starts with the following…

[CODE]CipherOutputStream cout = new CipherOutputStream(new BufferedOutputStream(out), enCipher);
DataInputStream din = new DataInputStream(new BufferedInputStream(in));
DataOutputStream dout = new DataOutputStream(cout);[/CODE]

This works beautifully, and I end up with a file full of encrypted text. However I’m running into a problem with the opposite code in my decrypt() method.

[CODE]CipherInputStream cin = new CipherInputStream(new BufferedInputStream(in), deCipher);
DataInputStream din = new DataInputStream(cin);
DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(out));[/CODE]

in is an InputStream passed to the method representing the file being decrypted. Calling in.available() with my current test file shows 32 bytes in the stream. When I create cin though it seems as though the contents of in are not making it through to the CipherInputStream. Calling cin.available() returns 0 bytes. Naturally this makes it a little hard to read the stream ? Anyone ever run into something like this? I’ve run into the same problem when not using the BufferedInputStream. Is there some method I should be calling after constructing the stream, to initialize it or something? Could my deCipher object be to blame?

to post a comment
Java

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliFeb 27.2007 — try to print out the contents first once you have the BufferedInputStream object and see if there is content to begin with. Then see if you are closing any stream object before you are creating this new object
Copy linkTweet thisAlerts:
@LenarynauthorFeb 27.2007 — I seem to have solved my own problem after finding a related issue on the Sun forums, here.

The initial problem was the condition on my DataInputStream read loop, although I wasn't using din.available() like the OP in the Sun thread. Using this method to check for the amount of data available in the stream led me further astray, since it only shows the number of bytes available without blocking. This is the read code I switched to.

[CODE]int numRead = 0;
while ((numRead = cin.read(buf)) >= 0) {
dout.write(buf, 2, numRead);
}[/CODE]


After switching to this I started to get not only file contents, but decrypted file contents ?

If you're wondering why I use 2 as my writing offset it's because I found that my DES encrypt method appears to stick a couple of bytes on the beginning of the encrypted stream.
×

Success!

Help @Lenaryn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...