Encryption/Decryption Streams Cause UTFDataFormatException



I'm working with pre-existing code on an open source project. The first
part of the program builds a jar file with a lot of classes, extra data and
several "packs", which are mini-archives themselves. On the "other end",
the mini-archives are read out from within the jar file and their
individual files are saved on disk.

This involves a LOT of code and some large classes, with a LOT of
dependencies, so I don't see how I can include runnable classes. I'm
hoping if I describe what is going on, someone who knows more about streams
or crypto classes will be able to tell me something that, to them, is
obvious, yet is something I don't know about. This is a pretty thorough
description of what is going on, so if it seems like I'm doing something
wrong or ignoring something, please tell me.

Data is written to the archives with a ComponentOutputStream and read from
the archives with a ComponentInputStream. In the first part, the
ComponentOutputStream is in a chain following a special output stream
designed to count the bytes that are written out in the stream. I have
added a class that is passed the ByteCountingOutputStream, chains a
CipherOutputStream after it, then chains the "regular"
ComponentOutputStream after that. This appears to work, since I don't get
any errors from this end, but, of course, it could be making a mess that
doesn't show until time to read and decrypt the archive. Sometimes the
archives are stored in files separate from the jar file, but I haven't even
gotten to testing that yet.

On the other end, code is read in from an InputStream that could be from a
URL on a network or it could be getting data from the archives within the
jar. A ComponentInputStream is wrapped around the regular InputStream and
data is read from that and files are written to the drive one by one as
they come out of the ComponentInputStream. I have handled this similar to
what I did with the other end: I have a class that is passed the
InputStream for each pack, chains the CipherInputStream after the
InputStream, then chains the ComponentInputStream after that.

In theory, this looks like it should work (at least to me), since components
are written, THEN encrypted, then stored in the archives and jar, and the
process is almost completely symmetrical on the other side.

It works fine for the first part of the first pack. Then, on reading what I
think is the last stored component (when written through the
ComponentOutputStream), I always get a UTFDataFormatException. I have
wondered if there is a problem with the output streams not being flushed
and/or closed after writing the pack, since it comes at the end of one
pack. There is a problem with this, since when the jar is being created,
it uses a primary stream which has to stay open. (If I'm wrong on this and
can close the streams, then re-open the output stream to the jar without
losing my place or anything in terms of compression or anything else, I'm
willing to see how.) I flush the last stream in the chain, the
ComponentOutputStream, which, if I understand, should force all the streams
to be flushed.

When the next pack is written, a new ComponentOutputStream (and new
CipherOutputStream and ByteCountingOutputStream) are created, chained after
the original main jar output stream.

It's similar on the other end: new InputStream, CipherInputStream, and
ComponenentInputStream are all created (in that order).

One last point: not all packs/archives are encrypted. I check the pack name
and see if it is listed as one to encrypt. If so, I include the
Cipher*putStream in the stream chain, if not, I don't.

Any help on this is appreciated. I'm not an expert in crypto classes and I
know there's a lot about streams I don't know -- I'm just not sure what
kind of things I should be checking or if there are tricks to flushing or
closing streams. (As I understand it, flushing or closing the last stream
in a chain closes or flushes them all.)

Thanks!

Hal
.



Relevant Pages

  • Re: Encryption/Decryption Streams Cause UTFDataFormatException SOLVED
    ... > part of the program builds a jar file with a lot of classes, ... > Data is written to the archives with a ComponentOutputStream and read from ... > designed to count the bytes that are written out in the stream. ... I flush the last stream in the chain, ...
    (comp.lang.java.help)
  • Re: How can app read its own Manifest.mf file ?
    ... a stream for the correct jar file. ... The only problem I hit was when reading the Manifest from WebStart I ...
    (comp.lang.java.programmer)
  • Re: URI is not hierarchical
    ... >> stream of data in the jar file. ... >> with the File object because it's not a real file. ... >> getResource() and then read the data from the resulting InputStream. ... Then create an output stream for writing the new file (which ...
    (comp.lang.java.programmer)
  • Re: Class Loading from Byte Stream
    ... > AFAICT, when you use a database, you don't store the jar, you store the ... stream to a JAR file, then load the classes from there. ...
    (comp.lang.java.help)