public class CipherInputStream
extends java.io.FilterInputStream
This class provides the same functionality as javax.crypto.CipherInputStream
but allows to specify a buffer size for supporting block oriented encoding as
required by PKCS#7.
This class extends the java.io.FilterInputStream
class for
combining the functionality of an InputStream and a Cipher.
According to the operation mode the Cipher has been initialized with, data
that is read from the underlying input stream will be returned en/decrypted
when calling one of the read()
methods.
You may use a CipherInputStream for reading encrypted data from a file thereby decrypting it for obtaining the plain text, e.g.:
KeyGenerator key_gen = KeyGenerator.getInstance("DES"); Key des_key = key_gen.generateKey(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, des_key); is = new FileInputStream("encrypted_data.enc"); CipherInputStream cis = new CipherInputStream(is, cipher); byte[] buffer = new byte[100]; int r; while ((r = cis.read(buffer)) != -1) { System.out.print(new String(buffer)); }
Modifier | Constructor and Description |
---|---|
protected |
CipherInputStream(java.io.InputStream is)
Creates a CipherInputStream only from an InputStream.
|
|
CipherInputStream(java.io.InputStream is,
javax.crypto.Cipher cipher)
Creates a CipherInputStream using an InputStream and a Cipher initialized
for either encryption or decryption.
|
|
CipherInputStream(java.io.InputStream is,
javax.crypto.Cipher cipher,
int bufferSize)
Creates a CipherInputStream using an InputStream, a Cipher initialized for
either encryption or decryption and a buffer size.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes available without blocking.
|
void |
close()
Closes this stream.
|
int |
read()
Reads the next data byte read from this input stream and returns it as an
int value between 0 and 255, or -1 if the the end of the stream already has
been reached.
|
int |
read(byte[] b)
Reads a number of data bytes from this input stream into a byte array.
|
int |
read(byte[] b,
int off,
int len)
Reads a specified number of data bytes from this input stream into a byte
array.
|
long |
skip(long n)
Skips over a specified number of data bytes of this input stream and
returns the number of bytes skipped.
|
public CipherInputStream(java.io.InputStream is, javax.crypto.Cipher cipher)
is
- the input streamcipher
- an initialized cipherpublic CipherInputStream(java.io.InputStream is, javax.crypto.Cipher cipher, int bufferSize)
is
- the input streamcipher
- an initialized cipherbufferSize
- size of the internal bufferprotected CipherInputStream(java.io.InputStream is)
is
- the input streampublic int read(byte[] b, int off, int len) throws java.io.IOException
off
. The decrypted data is unpadded only if the end (EOF) of
the underlying input stream is reached.read
in class java.io.FilterInputStream
b
- the byte array into which the data bytes are readoff
- the start offset of the datalen
- the maximum number of bytes to be readjava.io.IOException
- if an I/O error occurspublic int read(byte[] b) throws java.io.IOException
read
in class java.io.FilterInputStream
b
- the byte array into which the data bytes are readjava.io.IOException
- if an I/O error occurspublic int read() throws java.io.IOException
read
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurspublic long skip(long n) throws java.io.IOException
Since the number of bytes actually skipped may not equal (i.e. shorter) the given number of bytes to skip, the number of bytes actually skipped are returned.
skip
in class java.io.FilterInputStream
n
- the number of data bytes to skipjava.io.IOException
- if an I/O error occurspublic int available() throws java.io.IOException
available
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurspublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterInputStream
java.io.IOException
- if an error occurs when closing the stream. If the
underlying Cipher is an AEAD Cipher, closing the
stream may cause finalizing the Cipher operation
(if not done so far) which may cause an Exception if
the mac value cannot be verified