A FilterOutputStream that encrypts or decrypts the data passing
through it.
This class has a constructor that takes a Cipher and an output
stream as arguments. The cipher is used to encrypt or decrypt all
data supplied via calls to one of the
write
methods.
The encryption/decryption result is written to the output stream.
For block ciphers, a buffer is used for receiving the data to be
encrypted or decrypted. The maximum number of bytes that may be
buffered at any given time is given by the
getBufferSize
method.
For byte-oriented stream ciphers, no buffering is done (and
getBufferSize()
returns 0).
To supply the bytes that need to be encrypted/decrypted, make one
or more calls to one of the
write
methods. After you
have supplied all the data, call
close()
to ensure
final processing is done.
Note: JavaSoft's JCE required calling
flush
for final
processing, rather than
close
. However, if you call
flush
and then write more data, there will not be
sufficient information available when reading in the stream (e.g.
using
CipherInputStream), to determine when the flush
happened. Unpadding can only work correctly if no further data is
written after the final processing, which means that
close
is the right method to trigger this processing. I'm not sure whether
JavaSoft's implementation works if only
close
is called,
but calling
flush
followed by
close
should
work in both implementations.
With a cipher in the ENCRYPT state, the number of bytes not yet
encrypted and written to the stream is kept between 0 and
cipher.getPlaintextBlockSize()-1
inclusive. When
close
is called, the final data is padded, encrypted, and
the result written to the output stream. If the cipher's padding scheme
is NONE and the final data does not comprise a complete plaintext
block, an
IllegalBlockSizeException is thrown.
With a cipher in the DECRYPT state, [DOCUMENT ME]. When
close
is called, an exact number of ciphertext blocks should be in
the buffer (otherwise an
IllegalBlockSizeException is
thrown). Those blocks are decrypted, unpadded, and written to the
output stream.
Note: calling methods of a cipher while it is being used by a
CipherInputStream (apart from methods that have no side-effects,
like
getAlgorithm()
,
get*BlockSize()
, etc.)
will probably result in incorrect or unexpected output.
Copyright © 1997
Systemics Ltd on behalf of the
Cryptix Development Team.
All rights reserved.
$Revision: 1.6 $