diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
commit | 2b976f5364cfdbc37d3086019d93483c983eb80b (patch) | |
tree | cb846af3fd1d43f9c2562a1fb2d06b997ad8f229 /core/src/main/java/org/bouncycastle/crypto/generators/DESedeKeyGenerator.java | |
parent | 5f714bd92fbd780d22406f4bc3681be005f6f04a (diff) |
initial reshuffle
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/generators/DESedeKeyGenerator.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/generators/DESedeKeyGenerator.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/generators/DESedeKeyGenerator.java b/core/src/main/java/org/bouncycastle/crypto/generators/DESedeKeyGenerator.java new file mode 100644 index 00000000..3cab983d --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/generators/DESedeKeyGenerator.java @@ -0,0 +1,56 @@ +package org.bouncycastle.crypto.generators; + +import org.bouncycastle.crypto.KeyGenerationParameters; +import org.bouncycastle.crypto.params.DESedeParameters; + +public class DESedeKeyGenerator + extends DESKeyGenerator +{ + /** + * initialise the key generator - if strength is set to zero + * the key generated will be 192 bits in size, otherwise + * strength can be 128 or 192 (or 112 or 168 if you don't count + * parity bits), depending on whether you wish to do 2-key or 3-key + * triple DES. + * + * @param param the parameters to be used for key generation + */ + public void init( + KeyGenerationParameters param) + { + this.random = param.getRandom(); + this.strength = (param.getStrength() + 7) / 8; + + if (strength == 0 || strength == (168 / 8)) + { + strength = DESedeParameters.DES_EDE_KEY_LENGTH; + } + else if (strength == (112 / 8)) + { + strength = 2 * DESedeParameters.DES_KEY_LENGTH; + } + else if (strength != DESedeParameters.DES_EDE_KEY_LENGTH + && strength != (2 * DESedeParameters.DES_KEY_LENGTH)) + { + throw new IllegalArgumentException("DESede key must be " + + (DESedeParameters.DES_EDE_KEY_LENGTH * 8) + " or " + + (2 * 8 * DESedeParameters.DES_KEY_LENGTH) + + " bits long."); + } + } + + public byte[] generateKey() + { + byte[] newKey = new byte[strength]; + + do + { + random.nextBytes(newKey); + + DESedeParameters.setOddParity(newKey); + } + while (DESedeParameters.isWeakKey(newKey, 0, newKey.length)); + + return newKey; + } +} |