1
package org.bouncycastle.crypto.paddings;
3
import java.security.SecureRandom;
5
import org.bouncycastle.crypto.InvalidCipherTextException;
8
* A padder that adds Trailing-Bit-Compliment padding to a block.
10
* This padding pads the block out with the compliment of the last bit
14
public class TBCPadding
15
implements BlockCipherPadding
18
* Initialise the padder.
20
* @param random - a SecureRandom if available.
22
public void init(SecureRandom random)
23
throws IllegalArgumentException
29
* Return the name of the algorithm the padder implements.
31
* @return the name of the algorithm the padder implements.
33
public String getPaddingName()
39
* add the pad bytes to the passed in block, returning the
40
* number of bytes added.
42
* Note: this assumes that the last block of plain text is always
43
* passed to it inside in. i.e. if inOff is zero, indicating the
44
* entire block is to be overwritten with padding the value of in
45
* should be the same as the last block of plain text.
48
public int addPadding(
52
int count = in.length - inOff;
57
code = (byte)((in[inOff - 1] & 0x01) == 0 ? 0xff : 0x00);
61
code = (byte)((in[in.length - 1] & 0x01) == 0 ? 0xff : 0x00);
64
while (inOff < in.length)
74
* return the number of pad bytes present in the block.
76
public int padCount(byte[] in)
77
throws InvalidCipherTextException
79
byte code = in[in.length - 1];
81
int index = in.length - 1;
82
while (index > 0 && in[index - 1] == code)
87
return in.length - index;