PaddedBufferedBlockCipherpublic class PaddedBufferedBlockCipher extends org.bouncycastle.crypto.BufferedBlockCipher A wrapper class that allows block ciphers to be used to process data in
a piecemeal fashion with padding. The PaddedBufferedBlockCipher
outputs a block only when the buffer is full and more data is being added,
or on a doFinal (unless the current block in the buffer is a pad block).
The default padding mechanism used is the one outlined in PKCS5/PKCS7. |
Fields Summary |
---|
BlockCipherPadding | padding |
Methods Summary |
---|
public int | doFinal(byte[] out, int outOff)Process the last block in the buffer. If the buffer is currently
full and padding needs to be added a call to doFinal will produce
2 * getBlockSize() bytes.
int blockSize = cipher.getBlockSize();
int resultLen = 0;
if (forEncryption)
{
if (bufOff == blockSize)
{
if ((outOff + 2 * blockSize) > out.length)
{
reset();
throw new DataLengthException("output buffer too short");
}
resultLen = cipher.processBlock(buf, 0, out, outOff);
bufOff = 0;
}
padding.addPadding(buf, bufOff);
resultLen += cipher.processBlock(buf, 0, out, outOff + resultLen);
reset();
}
else
{
if (bufOff == blockSize)
{
resultLen = cipher.processBlock(buf, 0, buf, 0);
bufOff = 0;
}
else
{
reset();
throw new DataLengthException("last block incomplete in decryption");
}
try
{
resultLen -= padding.padCount(buf);
System.arraycopy(buf, 0, out, outOff, resultLen);
}
finally
{
reset();
}
}
return resultLen;
| public int | getOutputSize(int len)return the minimum size of the output buffer required for an update
plus a doFinal with an input of len bytes.
int total = len + bufOff;
int leftOver = total % buf.length;
if (leftOver == 0)
{
if (forEncryption)
{
return total + buf.length;
}
return total;
}
return total - leftOver + buf.length;
| public int | getUpdateOutputSize(int len)return the size of the output buffer required for an update
an input of len bytes.
int total = len + bufOff;
int leftOver = total % buf.length;
if (leftOver == 0)
{
return total - buf.length;
}
return total - leftOver;
| public void | init(boolean forEncryption, org.bouncycastle.crypto.CipherParameters params)initialise the cipher.
this.forEncryption = forEncryption;
reset();
if (params instanceof ParametersWithRandom)
{
ParametersWithRandom p = (ParametersWithRandom)params;
padding.init(p.getRandom());
cipher.init(forEncryption, p.getParameters());
}
else
{
padding.init(null);
cipher.init(forEncryption, params);
}
| public int | processByte(byte in, byte[] out, int outOff)process a single byte, producing an output block if neccessary.
int resultLen = 0;
if (bufOff == buf.length)
{
resultLen = cipher.processBlock(buf, 0, out, outOff);
bufOff = 0;
}
buf[bufOff++] = in;
return resultLen;
| public int | processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)process an array of bytes, producing output if necessary.
if (len < 0)
{
throw new IllegalArgumentException("Can't have a negative input length!");
}
int blockSize = getBlockSize();
int length = getUpdateOutputSize(len);
if (length > 0)
{
if ((outOff + length) > out.length)
{
throw new DataLengthException("output buffer too short");
}
}
int resultLen = 0;
int gapLen = buf.length - bufOff;
if (len > gapLen)
{
System.arraycopy(in, inOff, buf, bufOff, gapLen);
resultLen += cipher.processBlock(buf, 0, out, outOff);
bufOff = 0;
len -= gapLen;
inOff += gapLen;
while (len > buf.length)
{
resultLen += cipher.processBlock(in, inOff, out, outOff + resultLen);
len -= blockSize;
inOff += blockSize;
}
}
System.arraycopy(in, inOff, buf, bufOff, len);
bufOff += len;
return resultLen;
|
|