public class DESKeyGenerator extends VarLengthKeyGenerator
An application uses
for creating a KeyGenerator object for the DES algorithm. For actually generating the requested secret DES key from the KeyGenerator object just created, an application calls theKeyGenerator des_key_gen = KeyGenerator.getInstance("DES");
generateKey
method after having initialized
the generator with some random seed or relied on the default system-provided
source of randomness:
SecretKey des_key = des_key_gen.generateKey();
Due to the way, the DES algorithm generates sub-keys for the several DES rounds, some initial keys may be classified as weak or semi-weak keys reducing the security of the DES cipher (see "Applied Cryptography", Bruce Schneier, ISBN 0-471-59756-2). This KeyGenerator class ensures to generate a strong DES key by repeating the key generation process as long as comparison with all the known weak and semi-weak DES keys will show that the just created key is not a strong one.
Constructor and Description |
---|
DESKeyGenerator() |
Modifier and Type | Method and Description |
---|---|
static void |
adjustParity(byte[] key,
int offset)
Adjusts the parity-bits of a DES key, supplied in a byte array beginning
at the given offset.
|
static boolean |
checkParity(byte[] key,
int offset,
boolean odd)
Checks if the given DES key, supplied in a byte array beginning
at the given offset, is parity adjusted.
|
protected javax.crypto.SecretKey |
engineGenerateKey()
Generates a DES key.
|
protected void |
engineInit(int strength,
java.security.SecureRandom random)
Initializes this key generator for the given strength with the given random seed.
|
engineInit, engineInit
protected void engineInit(int strength, java.security.SecureRandom random)
engineInit
in class VarLengthKeyGenerator
strength
- the strength of the key to be created; if -1, the default length (64) is usedrandom
- the random seedjava.security.InvalidParameterException
- if the supplied key strength is not -1 or 56 or 64protected javax.crypto.SecretKey engineGenerateKey()
For ensuring to create a strong DES key, the key generation process is repeated as long as comparison with all the known weak and semi-weak DES keys will show that the just created key is not a strong DES key.
engineGenerateKey
in class VarLengthKeyGenerator
SecretKey
public static void adjustParity(byte[] key, int offset)
key
- the byte array holding the DES keyoffset
- the offset indicating where the key starts within the given
byte arraypublic static boolean checkParity(byte[] key, int offset, boolean odd)
key
- the byte array holding the DES keyoffset
- the offset indicating where the key starts within the given
byte arrayodd
- whether to check for odd or even paritytrue
if checking for odd parity and the key is odd parity adjusted;
true
if checking for even parity and the key is even parity adjusted;
false
in any other case (note that if this method returns false when
checking for odd (even) parity, this does not necessarily mean
that the key is even (odd) parity adjusted