    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 class CipherOutputStream
extends FilterOutputStream

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, CipherInputStream

##

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`.

##

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

##

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.

##

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

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

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