    All Packages  Class Hierarchy  This Package  Previous  Next  Index

# Class java.security.CipherInputStream

    java.lang.Object
       |
       +----java.io.InputStream
               |
               +----java.io.FilterInputStream
                       |
                       +----java.security.CipherInputStream

public class CipherInputStream
extends FilterInputStream

A FilterInputStream that encrypts or decrypts the data passing through it. Typically, this stream would be used as a filter to read an encrypted file.

This class has a constructor that takes a Cipher and an input stream as arguments. The cipher is used to encrypt or decrypt all data read through the stream.

For block ciphers, some buffering of the data received from the input stream is done. The buffer length is the cipher's block size. For stream ciphers, that is, ciphers capable of encrypting or decrypting a byte at a time, no buffering is necessary.

The data is encrypted or decrypted, depending on the initialization state of the Cipher. To get the encryption/decryption result bytes, make one or more calls to the `read` methods. One read method, with no arguments, returns the next result byte. The other read method returns multiple result bytes at once.

For a buffered decrypter stream, every time the buffer is empty, the stream attempts to read a complete buffer, and blocks until it does. When it has read a buffer, it decrypts it. If the next byte is an EOF and the cipher is a padding/unpadding cipher, the decryption result is then unpadded. If an EOF is encountered before a whole buffer has been read, an exception is thrown.

For a buffered encrypter stream, every time the buffer is empty, the stream attempts to read a complete buffer, and blocks until it does. If it reads a whole buffer without reaching EOF, it encrypts it. If it reaches EOF in the middle of a buffer, and the cipher is a padding cipher, it will pad the buffer, and encrypt it. Otherwise, an exception will be thrown.

See Also:
  Cipher, blockSize, CipherOutputStream

##

CipherInputStream(InputStream, Cipher)
  Constructs an input stream using a cipher that must be initialized for either encryption or decryption, that is, a cipher whose state is either `ENCRYPT` or `DECRYPT`.

##

read()
  Returns the next encrypted or decrypted byte, depending on the cipher state.
read(byte[], int, int)
  Fills up the specified bytes of the `b` array with the next `len` encrypted or decrypted bytes (depending on the cipher state).

##

CipherInputStream

     public CipherInputStream(InputStream in,
                              Cipher cipher)

  Constructs an input stream using a cipher that must be initialized for either encryption or decryption, that is, a cipher whose state is either `ENCRYPT` or `DECRYPT`.

  Parameters:
    is - the input stream.
    cipher - an initialized cipher.
  See Also:
    Cipher

##

read

     public int read(byte b[],
                     int off,
                     int len) throws IOException

  Fills up the specified bytes of the `b` array with the next `len` encrypted or decrypted bytes (depending on the cipher state).

  Parameters:
    b - the byte array into which the encrypted or decrypted bytes will be read.
    off - the offset into `b` indicating where the first encrypted or decrypted byte should be read.
    len - the number of encrypted/decrypted bytes to read into `b`, starting at offset `off`.
  Returns:
    the number of bytes read into `b`, or -1 if fewer than `len` encrypted/decrypted bytes remained.
  Throws: IOException
    if an error occurs.
  Overrides:
    read in class FilterInputStream
  See Also:
    ENCRYPT, DECRYPT, getState

read

     public int read() throws IOException

  Returns the next encrypted or decrypted byte, depending on the cipher state.

  Returns:
    the next encrypted or decrypted byte, or -1 if the last encrypted/decrypted byte was already returned.
  Throws: IOException
    if an error occurs.
  Overrides:
    read in class FilterInputStream
  See Also:
    ENCRYPT, DECRYPT, getState

    All Packages  Class Hierarchy  This Package  Previous  Next  Index