📄 desedekeygenerator.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: DESedeKeyGenerator.java,v 1.10 1999/02/17 04:31:52 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/DESedeKeyGenerator.java,v $
* $Revision: 1.10 $
* $Date: 1999/02/17 04:31:52 $
* $State: Exp $
*/
import java.security.SecureRandom;
import java.security.InvalidKeyException;
import javax.crypto.SecretKey;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
/**
* This class is used for generating random DESede keys. This class
* should not be instantiated directly, instead use the
* javax.crypto.KeyGenerator interface.
* <p>
* There is no AlgorithmParameterSpec class defined for DESede 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 DESedeKeyGenerator extends DESKeyGenerator
{
public final static String ident = "$Id: DESedeKeyGenerator.java,v 1.10 1999/02/17 04:31:52 leachbj Exp $";
/**
* Generates a secret key, setting odd parity and checking that the
* key is not a weak key.
*
* @return a DESede key object.
*/
protected SecretKey engineGenerateKey()
{
/*
* use a default random source if none was provided
*/
if (rand == null)
{
rand = new SecureRandom();
}
// generate random non-weak key
int count = 0;
byte[] keyBytes = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
byte[] bytes = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
DESedeKey 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))
{
continue;
}
}
catch (InvalidKeyException e)
{
// strictly speaking this can never happen
// but just in case...
throw new RuntimeException(
"DESedeKeyGenerator: " + e);
}
System.arraycopy(bytes, 0, keyBytes,
count++ * DESKeySpec.DES_KEY_LEN,
DESKeySpec.DES_KEY_LEN);
/*
* clear the key from the
* byte buffer
*/
for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++)
{
bytes[i] = 0;
}
if (count == 3)
{
key = new DESedeKey(keyBytes);
/*
* clear the key from the
* byte buffer
*/
for (int i = 0;
i < DESedeKeySpec.DES_EDE_KEY_LEN; i++)
{
keyBytes[i] = 0;
}
}
}
while (key == null);
return key;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -