All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.security.CipherOutputStream

java.lang.Object
   |
   +----java.io.OutputStream
           |
           +----java.io.FilterOutputStream
                   |
                   +----java.security.CipherOutputStream

public synchronized class CipherOutputStream
extends FilterOutputStream
ATTENTION:

This is NOT the implementation from Sun. This class has been developed by IAIK according to the documentation publically available. Only the documentation from Sun has been inserted into the source files.


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.

A buffer is used for receiving the data to be encrypted or decrypted, depending on the state of the Cipher. 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 flush to ensure final processing is done.

With an encrypter stream, every time a complete block has been supplied via calls to write, the block is encrypted and written to the output stream. When flush is called, the final data is encrypted and the result written to the output stream. If the final data does not comprise a complete block, and the cipher is a padding cipher, the data is padded before encryption. If padding is needed but the cipher is not a padding cipher, an exception is thrown.

With a decrypter stream, every time a complete block plus one byte has been supplied via calls to write, the block is decrypted and written to the output stream. When flush is called, exactly one block should be in the buffer. It is decrypted and written out.

See Also:
Cipher

Constructor Index

 o CipherOutputStream(OutputStream, Cipher)
Constructs an output stream using a cipher that must be initialized for encryption or decryption, that is, a cipher whose state is either ENCRYPT or DECRYPT.

Method Index

 o flush()
Performs final processing and output to the output stream of the final encryption or decryption result bytes.
 o write(byte[], int, int)
Supplies bytes to be used for encryption or decryption, depending on the cipher state.
 o write(int)
Supplies a byte to be used for encryption or decryption, depending on the cipher state.

Constructors

 o CipherOutputStream
 public CipherOutputStream(OutputStream os,
                           Cipher cipher)
Constructs an output stream using a cipher that must be initialized for encryption or decryption, that is, a cipher whose state is either ENCRYPT or DECRYPT.

Parameters:
os - - the output stream.
cipher - - an initialized cipher.

Methods

 o write
 public synchronized void write(int b) throws IOException
Supplies a byte to be used for encryption or decryption, depending on the cipher state.

Parameters:
b - - the byte to be used for encryption or decryption.
Throws: IOException
if an error occurs.
Overrides:
write in class FilterOutputStream
 o write
 public synchronized void write(byte b[],
                                int off,
                                int len) throws IOException
Supplies bytes to be used for encryption or decryption, depending on the cipher state.

Parameters:
b - - the buffer containing the bytes to be used for encryption or decryption.
off - - the offset into b indicating the location of the first byte to be used.
len - - the number of bytes to read from b, starting at offset off.
Throws: IOException
If an error occurs.
Overrides:
write in class FilterOutputStream
 o flush
 public synchronized void flush() throws IOException
Performs final processing and output to the output stream of the final encryption or decryption result bytes.

For an encrypter stream, the final data is encrypted and the result written to the output stream. If the final data does not comprise a complete block, and the cipher is a padding cipher, the data is padded before encryption. If padding is needed but the cipher is not a padding cipher, an exception is thrown.

For a decrypter stream, when flush is called, exactly one block should be in the buffer. It is decrypted and written out.

Throws: IOException
If an error occurs.
Overrides:
flush in class FilterOutputStream

All Packages  Class Hierarchy  This Package  Previous  Next  Index