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 the padding according to the scheme referenced in
09: * ISO 7814-4 - scheme 2 from ISO 9797-1. The first byte is 0x80, rest is 0x00
10: */
11: public class ISO7816d4Padding implements BlockCipherPadding {
12: /**
13: * Initialise the padder.
14: *
15: * @param random - a SecureRandom if available.
16: */
17: public void init(SecureRandom random)
18: throws IllegalArgumentException {
19: // nothing to do.
20: }
21:
22: /**
23: * Return the name of the algorithm the padder implements.
24: *
25: * @return the name of the algorithm the padder implements.
26: */
27: public String getPaddingName() {
28: return "ISO7816-4";
29: }
30:
31: /**
32: * add the pad bytes to the passed in block, returning the
33: * number of bytes added.
34: */
35: public int addPadding(byte[] in, int inOff) {
36: int added = (in.length - inOff);
37:
38: in[inOff] = (byte) 0x80;
39: inOff++;
40:
41: while (inOff < in.length) {
42: in[inOff] = (byte) 0;
43: inOff++;
44: }
45:
46: return added;
47: }
48:
49: /**
50: * return the number of pad bytes present in the block.
51: */
52: public int padCount(byte[] in) throws InvalidCipherTextException {
53: int count = in.length - 1;
54:
55: while (count > 0 && in[count] == 0) {
56: count--;
57: }
58:
59: if (in[count] != (byte) 0x80) {
60: throw new InvalidCipherTextException("pad block corrupted");
61: }
62:
63: return in.length - count;
64: }
65: }
|