?? pbekeyfactory.java
字號:
package au.net.aba.crypto.provider;
/*
* $Id: PBEKeyFactory.java,v 1.3 1999/01/28 04:47:28 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/PBEKeyFactory.java,v $
* $Revision: 1.3 $
* $Date: 1999/01/28 04:47:28 $
* $State: Exp $
*/
import javax.crypto.spec.PBEKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.InvalidKeyException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;
/**
* This class is used to convert password based keys into a format
* usable by the ABA provider. This class can convert PBEKeySpec
* objects into an ABA PBEKey and visa versa.
* <p>
* This class should not be instantiated directly, instead use the
* java.security.KeyFactory interface.
*
* @see java.security.KeyFactory
*/
public class PBEKeyFactory extends SecretKeyFactorySpi
{
public final static String ident = "$Id: PBEKeyFactory.java,v 1.3 1999/01/28 04:47:28 leachbj Exp $";
/**
* Generates a PBE SecretKey object from the provided key specification
* (key material). This class supports the PBEKeySpec KeySpec class.
*
* @returns The secret key.
* @exception InvalidKeySpecException The provided KeySpec was not
* a PBEKeySpec KeySpec.
*/
protected SecretKey engineGenerateSecret(
KeySpec keySpec)
throws InvalidKeySpecException
{
if (keySpec == null || !(keySpec instanceof PBEKeySpec))
{
throw new InvalidKeySpecException("Invalid KeySpec");
}
char[] password = ((PBEKeySpec)keySpec).getPassword();
return new PBEKey(password);
}
/**
* Returns a specification (key material) of the given key object in
* the requested format. Currently only the PBEKeySpec KeySpec
* class is supported.
*
* @param key the key
* @param keySpec the requested format in which the key material shall
* be returned, must be PBEKeySpec.class
* @returns the underlying key specification (key material) in the
* requested format
* @exception InvalidKeySpecException if the provided key
* is not a PBEKey or the requested KeySpec is not a
* PBEKeySpec.
*/
protected KeySpec engineGetKeySpec(
SecretKey key,
Class keySpec)
throws InvalidKeySpecException
{
if (key instanceof PBEKey)
{
if (keySpec.equals(PBEKeySpec.class))
{
PBEKey pbeKey = (PBEKey)key;
return new PBEKeySpec(pbeKey.getPassword());
}
}
throw new InvalidKeySpecException(
"Unsupported KeySpec requested.");
}
/**
* Translates a PBE key object, whose provider may be unknown or
* potentially untrusted, into a corresponding key object of this key
* factory.
*
* @param key - the key whose provider is unknown or untrusted
* @returns the translated key
* @exception InvalidKeyException if the given key cannot be processed
* by this key factory.
*/
protected SecretKey engineTranslateKey(
SecretKey key)
throws InvalidKeyException
{
if (!key.getAlgorithm().equals("PBE"))
{
throw new InvalidKeyException("not a PBE key");
}
// To do this requires translating bytes into chars!
byte[] bytes = key.getEncoded();
char[] pwd = new char[bytes.length];
for (int i = 0; i < pwd.length; i++)
pwd[i] = (char)bytes[i];
return new PBEKey(pwd);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -