?? rc4keyfactory.java
字號:
package au.net.aba.crypto.provider;
/*
* $Id: RC4KeyFactory.java,v 1.12 1999/02/18 06:28:50 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/RC4KeyFactory.java,v $
* $Revision: 1.12 $
* $Date: 1999/02/18 06:28:50 $
* $State: Exp $
*/
import au.net.aba.crypto.spec.RC4KeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.InvalidKeyException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;
import javax.crypto.spec.SecretKeySpec;
/**
* This class is used to convert RC4 keys into a format
* usable by the ABA provider. The supported KeySpec classes are
* RC4KeySpec and SecretKeySpec. In the case of the SecretKeySpec
* the key data must not be encoded. The supported Key class is
* RC4Key.
* <p>
* This class should not be instantiated directly, instead use the
* java.security.KeyFactory interface.
*
* @see java.security.KeyFactory
*/
public class RC4KeyFactory extends SecretKeyFactorySpi
{
public final static String ident = "$Id: RC4KeyFactory.java,v 1.12 1999/02/18 06:28:50 leachbj Exp $";
/**
* Generates a RC4 SecretKey object from the provided key
* specification (key material). This class supports the
* RC4KeySpec and SecretKeySpec KeySpec classes.
*
* @returns The secret key.
* @exception InvalidKeySpecException The provided KeySpec was not
* a RC4KeySpec or SecretKeySpec KeySpec instance.
*/
protected SecretKey engineGenerateSecret(
KeySpec keySpec)
throws InvalidKeySpecException
{
if (keySpec instanceof RC4KeySpec)
{
RC4KeySpec rc4Kspec = (RC4KeySpec)keySpec;
return new RC4Key(rc4Kspec.getKey());
}
else if (keySpec instanceof SecretKeySpec)
{
SecretKeySpec skspec = (SecretKeySpec)keySpec;
if (!"RC4".equals(skspec.getAlgorithm()))
{
throw new InvalidKeySpecException(
"SecretKeySpec not for RC4");
}
if (!"RAW".equals(skspec.getFormat()))
{
throw new InvalidKeySpecException(
"Unknown format in KeySpec");
}
return new RC4Key(skspec.getEncoded());
}
throw new InvalidKeySpecException("Invalid KeySpec");
}
/**
* Returns a specification (key material) of the given key object in
* the requested format.
* <p>
* Can convert from an ABA RC4Key into a SecretKeySpec or
* RC4KeySpec class.
*
* @param key the key
* @param keySpec the requested format in which the key material shall
* be returned
* @returns the underlying key specification (key material) in the
* requested format
* @exception InvalidKeySpecException if the requested key
* specification is inappropriate for the given key, or the given
* key cannot be dealt with (e.g., the given key has an
* unrecognised format).
*/
protected KeySpec engineGetKeySpec(
SecretKey key,
Class keySpec)
throws InvalidKeySpecException
{
if (key instanceof RC4Key)
{
if (SecretKeySpec.class.isAssignableFrom(keySpec))
{
return new SecretKeySpec(key.getEncoded(),
"RC4");
}
else if (RC4KeySpec.class.isAssignableFrom(keySpec))
{
try
{
return new RC4KeySpec(key.getEncoded());
}
catch (InvalidKeyException e)
{
throw new InvalidKeySpecException(
e.getMessage());
}
}
}
throw new InvalidKeySpecException("not implemented");
}
/**
* Translates a RC4 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("RC4"))
{
throw new InvalidKeyException("not a RC4 key");
}
return new RC4Key(key.getEncoded());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -