1 package org.bouncycastle.crypto;
4 * a wrapper for block ciphers with a single byte block size, so that they
5 * can be treated like stream ciphers.
7 public class StreamBlockCipher
8 implements StreamCipher
10 private BlockCipher cipher;
12 private byte[] oneByte = new byte[1];
17 * @param cipher the block cipher to be wrapped.
18 * @exception IllegalArgumentException if the cipher has a block size other than
21 public StreamBlockCipher(
24 if (cipher.getBlockSize() != 1)
26 throw new IllegalArgumentException("block cipher block size != 1.");
33 * initialise the underlying cipher.
35 * @param forEncryption true if we are setting up for encryption, false otherwise.
36 * @param param the necessary parameters for the underlying cipher to be initialised.
39 boolean forEncryption,
40 CipherParameters params)
42 cipher.init(forEncryption, params);
46 * return the name of the algorithm we are wrapping.
48 * @return the name of the algorithm we are wrapping.
50 public String getAlgorithmName()
52 return cipher.getAlgorithmName();
56 * encrypt/decrypt a single byte returning the result.
58 * @param in the byte to be processed.
59 * @return the result of processing the input byte.
61 public byte returnByte(
66 cipher.processBlock(oneByte, 0, oneByte, 0);
72 * process a block of bytes from in putting the result into out.
74 * @param in the input byte array.
75 * @param inOff the offset into the in array where the data to be processed starts.
76 * @param len the number of bytes to be processed.
77 * @param out the output buffer the processed bytes go into.
78 * @param outOff the offset into the output byte array the processed data stars at.
79 * @exception DataLengthException if the output buffer is too small.
81 public void processBytes(
87 throws DataLengthException
89 if (outOff + len > out.length)
91 throw new DataLengthException("output buffer too small in processBytes()");
94 for (int i = 0; i != len; i++)
96 cipher.processBlock(in, inOff + i, out, outOff + i);
101 * reset the underlying cipher. This leaves it in the same state
102 * it was at after the last init (if there was one).