1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.crypto.generators; 2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.CipherKeyGenerator; 4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.crypto.KeyGenerationParameters; 5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DESParameters; 6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DESKeyGenerator 8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam extends CipherKeyGenerator 9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{ 10c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom /** 11c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * initialise the key generator - if strength is set to zero 12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * the key generated will be 64 bits in size, otherwise 13c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * strength can be 64 or 56 bits (if you don't count the parity bits). 14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * 15c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * @param param the parameters to be used for key generation 16c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom */ 17c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom public void init( 18c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom KeyGenerationParameters param) 19c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 20c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom super.init(param); 21c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 22c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom if (strength == 0 || strength == (56 / 8)) 23c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 24c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom strength = DESParameters.DES_KEY_LENGTH; 25c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 26c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom else if (strength != DESParameters.DES_KEY_LENGTH) 27c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom { 28c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom throw new IllegalArgumentException("DES key must be " 29c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom + (DESParameters.DES_KEY_LENGTH * 8) 30c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom + " bits long."); 31c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 32c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom } 33c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public byte[] generateKey() 35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] newKey = new byte[DESParameters.DES_KEY_LENGTH]; 37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam do 39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam random.nextBytes(newKey); 41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DESParameters.setOddParity(newKey); 43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam while (DESParameters.isWeakKey(newKey, 0)); 45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return newKey; 47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam} 49