01: package org.bouncycastle.crypto.generators;
02:
03: import org.bouncycastle.crypto.KeyGenerationParameters;
04: import org.bouncycastle.crypto.params.DESedeParameters;
05:
06: public class DESedeKeyGenerator extends DESKeyGenerator {
07: /**
08: * initialise the key generator - if strength is set to zero
09: * the key generated will be 192 bits in size, otherwise
10: * strength can be 128 or 192 (or 112 or 168 if you don't count
11: * parity bits), depending on whether you wish to do 2-key or 3-key
12: * triple DES.
13: *
14: * @param param the parameters to be used for key generation
15: */
16: public void init(KeyGenerationParameters param) {
17: super .init(param);
18:
19: if (strength == 0 || strength == (168 / 8)) {
20: strength = DESedeParameters.DES_EDE_KEY_LENGTH;
21: } else if (strength == (112 / 8)) {
22: strength = 2 * DESedeParameters.DES_KEY_LENGTH;
23: } else if (strength != DESedeParameters.DES_EDE_KEY_LENGTH
24: && strength != (2 * DESedeParameters.DES_KEY_LENGTH)) {
25: throw new IllegalArgumentException("DESede key must be "
26: + (DESedeParameters.DES_EDE_KEY_LENGTH * 8)
27: + " or "
28: + (2 * 8 * DESedeParameters.DES_KEY_LENGTH)
29: + " bits long.");
30: }
31: }
32:
33: public byte[] generateKey() {
34: byte[] newKey = new byte[strength];
35:
36: do {
37: random.nextBytes(newKey);
38:
39: DESedeParameters.setOddParity(newKey);
40: } while (DESedeParameters.isWeakKey(newKey, 0, newKey.length));
41:
42: return newKey;
43: }
44: }
|