01: package org.bouncycastle.crypto.paddings;
02:
03: import java.security.SecureRandom;
04:
05: import org.bouncycastle.crypto.InvalidCipherTextException;
06:
07: /**
08: * A padder that adds ISO10126-2 padding to a block.
09: */
10: public class ISO10126d2Padding implements BlockCipherPadding {
11: SecureRandom random;
12:
13: /**
14: * Initialise the padder.
15: *
16: * @param random a SecureRandom if available.
17: */
18: public void init(SecureRandom random)
19: throws IllegalArgumentException {
20: if (random != null) {
21: this .random = random;
22: } else {
23: this .random = new SecureRandom();
24: }
25: }
26:
27: /**
28: * Return the name of the algorithm the padder implements.
29: *
30: * @return the name of the algorithm the padder implements.
31: */
32: public String getPaddingName() {
33: return "ISO10126-2";
34: }
35:
36: /**
37: * add the pad bytes to the passed in block, returning the
38: * number of bytes added.
39: */
40: public int addPadding(byte[] in, int inOff) {
41: byte code = (byte) (in.length - inOff);
42:
43: while (inOff < (in.length - 1)) {
44: in[inOff] = (byte) random.nextInt();
45: inOff++;
46: }
47:
48: in[inOff] = code;
49:
50: return code;
51: }
52:
53: /**
54: * return the number of pad bytes present in the block.
55: */
56: public int padCount(byte[] in) throws InvalidCipherTextException {
57: int count = in[in.length - 1] & 0xff;
58:
59: if (count > in.length) {
60: throw new InvalidCipherTextException("pad block corrupted");
61: }
62:
63: return count;
64: }
65: }
|