javax.crypto
Class CipherOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--java.io.FilterOutputStream
              |
              +--javax.crypto.CipherOutputStream

public class CipherOutputStream
extends FilterOutputStream

Class for en/decrypting data to be written to an output stream.


Attention:  This is not a SUN implementation!

This class has been developed by IAIK according to the documentation publically available.
For SUNīs documentation of this class see http://java.sun.com/security/JCE1.2/spec/apidoc/index.html


This class extends the java.io.FilterOutputStream class for combining the functionality of a OutputStream and a Cipher. According to the operation mode the Cipher has been initialized with, data is en/decrypted before writing it to the underlying stream when calling one of the write() methods.

An application may use this class for encrypting and writing data to a file.

Version:
File Revision 17
See Also:
OutputStream, FilterOutputStream, Cipher, CipherInputStream

Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
CipherOutputStream(OutputStream os)
          Creates a CipherOutputStream only from an OutputStream.
CipherOutputStream(OutputStream os, Cipher cipher)
          Creates a CipherOutputStream using an OutputStream and a Cipher initialized either for encryption or decryption.
 
Method Summary
 void close()
          Calls the cipher for the last time and closes the output stream.
 void write(byte[] b)
          Writes the bytes from the given byte array to this output stream.
 void write(byte[] b, int off, int len)
          Writes a specified number of bytes from a given position of the given byte array to this output stream.
 void write(int b)
          Writes a byte to the underlying output stream.
 
Methods inherited from class java.io.FilterOutputStream
flush
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CipherOutputStream

public CipherOutputStream(OutputStream os,
                          Cipher cipher)
Creates a CipherOutputStream using an OutputStream and a Cipher initialized either for encryption or decryption.
Parameters:
os - the output stream.
cipher - an initialized cipher.

CipherOutputStream

public CipherOutputStream(OutputStream os)
Creates a CipherOutputStream only from an OutputStream. Since no Cipher is specified, alternatively you may create a CipherOutputStream using an OutputStream and a NullCipher.
Parameters:
os - the output stream
Method Detail

write

public void write(int b)
           throws IOException
Writes a byte to the underlying output stream. Depending on whether the supplied Cipher has been initialized for encryption or decryption, the given byte is processed accordingly before writing it to the stream.
Overrides:
write in class FilterOutputStream
Parameters:
b - the byte to be written to this stream
Throws:
IOException - if an I/O error occurs

write

public void write(byte[] b)
           throws IOException
Writes the bytes from the given byte array to this output stream. Depending on whether the supplied Cipher has been initialized for encryption or decryption, the bytes are processed accordingly before writing it to the stream.
Overrides:
write in class FilterOutputStream
Parameters:
b - the byte array containing the data to be written to this stream
Throws:
IOException - if an I/O error occurs.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Writes a specified number of bytes from a given position of the given byte array to this output stream. Depending on whether the supplied Cipher has been initialized for encryption or decryption, the bytes are processed accordingly before writing it to the stream.
Overrides:
write in class FilterOutputStream
Parameters:
b - the byte array containing the data to be written to this stream
off - the start offset indicating the position within the given byte array b from which the data is to be taken for en/decrypting and writing it to the stream
len - the number of bytes to write this stream
Throws:
IOException - if an I/O error occurs

close

public void close()
           throws IOException
Calls the cipher for the last time and closes the output stream. Always call close() on a CipherOutputStream because this method calls doFinal(), which appends/removes the padding, on the crypted data.
Overrides:
close in class FilterOutputStream
Throws:
IOException - if an I/O error occurs

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK