📄 deskeygenerator.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: DESKeyGenerator.java,v 1.14 1998/10/29 06:16:09 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/DESKeyGenerator.java,v $
* $Revision: 1.14 $
* $Date: 1998/10/29 06:16:09 $
* $State: Exp $
*/
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.SecureRandom;
import java.security.InvalidKeyException;
import javax.crypto.KeyGeneratorSpi;
import javax.crypto.SecretKey;
import javax.crypto.spec.DESKeySpec;
/**
* This class is used for generating random DES keys. This class
* should not be instantiated directly, instead use the
* javax.crypto.KeyGenerator interface.
* <p>
* There is no AlgorithmParameterSpec class defined for DES so this
* generator can only be initialised using the keysize,random
* initialisation.
* <p>
* The returned key will be a non-weak key with odd parity.
*/
public class DESKeyGenerator extends KeyGeneratorSpi
{
public final static String ident = "$Id: DESKeyGenerator.java,v 1.14 1998/10/29 06:16:09 leachbj Exp $";
SecureRandom rand;
/**
* Generates a secret key, setting odd parity and confirming that the
* key is not a weak key.
*
* @return a secret key representing a DES key.
*/
protected SecretKey engineGenerateKey()
{
/*
* Use a default random source if none is provided
*/
if (rand == null)
{
rand = new SecureRandom();
}
// generate random non-weak key
byte[] bytes = new byte[DESKeySpec.DES_KEY_LEN];
DESKey key = null;
do {
// get 64 bits of random data, 8 bits are wasted :(
rand.nextBytes(bytes);
// set the parity to odd
setOddParity(bytes);
// check for weak keys
try
{
if (!DESKeySpec.isWeak(bytes, 0))
{
key = new DESKey(bytes);
// clear the key from the byte buffer
for (int i = 0;
i < DESKeySpec.DES_KEY_LEN; i++)
{
bytes[i] = 0;
}
}
}
catch (InvalidKeyException e)
{
// strictly speaking this can never happen
// but just in case...
throw new RuntimeException(
"DESKeyGenerator: " + e);
}
} while (key == null);
return key;
}
/**
* Since DES keys are of a fixed size, this method does
* nothing except set the random source.
*
* @param strength the strength of the key. This parameter is
* ignored.
* @param random the source of randomness for this key generator
*/
protected void engineInit(
int strength,
SecureRandom random)
{
rand = random;
}
/**
* Initialises the key generator with the given random number source.
*
* @param random a source of random numbers for this generator.
*/
protected void engineInit(
SecureRandom random)
{
rand = random;
}
/**
* This method is not implemented as there is no AlgorithmParameterSpec
* defined for DES. (Use one of the other initialisation methods!)
*
* @param params the algorithm parameter specs for this
* generator.
* @param random a source of random numbers for this generator.
* @exception InvalidAlgorithmParameterException An invalid
* parameter specification is provided.
*/
protected void engineInit(
AlgorithmParameterSpec params,
SecureRandom random)
throws InvalidAlgorithmParameterException
{
throw new InvalidAlgorithmParameterException("Not Implemented");
}
/**
* DES Keys use the LSB as the odd parity bit. This can
* be used to check for corrupt keys.
*
* @param bytes the byte array to set the parity on.
*/
protected void setOddParity(byte[] bytes)
{
for (int i = 0; i < bytes.length; i++)
{
int b = bytes[i];
bytes[i] = (byte)((b & 0xfe) |
(((b >> 1) ^
(b >> 2) ^
(b >> 3) ^
(b >> 4) ^
(b >> 5) ^
(b >> 6) ^
(b >> 7)) ^ 0x01));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -