Most visited

Recently visited

Added in API level 1

KeyFactory

public class KeyFactory
extends Object

java.lang.Object
   ↳ java.security.KeyFactory


密钥工厂用于将 密钥Key类型的不透明密码密钥)转换为 密钥规范 (底层密钥资料的透明表示),反之亦然。

主要工厂是双向的。 也就是说,它们允许您从给定的密钥规范(密钥材料)构建不透明的密钥对象,或者以合适的格式检索密钥对象的基础密钥材料。

同一个密钥可能存在多个兼容密钥规范。 例如,可以使用DSAPublicKeySpecX509EncodedKeySpec来指定DSA公钥。 一个关键工厂可以用来在兼容的密钥规范之间进行转换。

以下是如何使用密钥工厂为了从其编码实例化DSA公钥的示例。 假设Alice收到了Bob的数字签名。 鲍勃还给她发了他的公钥(编码格式)以验证他的签名。 Alice然后执行以下操作:

 X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
 PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
 Signature sig = Signature.getInstance("DSA");
 sig.initVerify(bobPubKey);
 sig.update(data);
 sig.verify(signature);
 

Android提供了以下 KeyFactory算法:

Name Supported (API Levels)
DH 1+
DSA 1+
EC 11+
RSA 1+
X.509 1–8
These algorithms are described in the KeyFactory section of the Java Cryptography Architecture Standard Algorithm Name Documentation.

也可以看看:

Summary

Protected constructors

KeyFactory(KeyFactorySpi keyFacSpi, Provider provider, String algorithm)

创建一个KeyFactory对象。

Public methods

final PrivateKey generatePrivate(KeySpec keySpec)

根据提供的密钥规范(密钥材料)生成私钥对象。

final PublicKey generatePublic(KeySpec keySpec)

根据提供的密钥规范(密钥材料)生成公钥对象。

final String getAlgorithm()

获取与此 KeyFactory关联的算法的名称。

static KeyFactory getInstance(String algorithm)

返回一个KeyFactory对象,用于转换指定算法的公钥/私钥。

static KeyFactory getInstance(String algorithm, String provider)

返回一个KeyFactory对象,用于转换指定算法的公钥/私钥。

static KeyFactory getInstance(String algorithm, Provider provider)

返回一个KeyFactory对象,用于转换指定算法的公钥/私钥。

final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec)

返回给定密钥对象的规范(密钥材料)。

final Provider getProvider()

返回此关键工厂对象的提供者。

final Key translateKey(Key key)

将提供者可能未知或可能不可信的密钥对象转换为此密钥工厂的相应密钥对象。

Inherited methods

From class java.lang.Object

Protected constructors

KeyFactory

Added in API level 1
KeyFactory (KeyFactorySpi keyFacSpi, 
                Provider provider, 
                String algorithm)

创建一个KeyFactory对象。

Parameters
keyFacSpi KeyFactorySpi: the delegate
provider Provider: the provider
algorithm String: the name of the algorithm to associate with this KeyFactory

Public methods

generatePrivate

Added in API level 1
PrivateKey generatePrivate (KeySpec keySpec)

根据提供的密钥规范(密钥材料)生成私钥对象。

Parameters
keySpec KeySpec: the specification (key material) of the private key.
Returns
PrivateKey the private key.
Throws
InvalidKeySpecException if the given key specification is inappropriate for this key factory to produce a private key.

generatePublic

Added in API level 1
PublicKey generatePublic (KeySpec keySpec)

根据提供的密钥规范(密钥材料)生成公钥对象。

Parameters
keySpec KeySpec: the specification (key material) of the public key.
Returns
PublicKey the public key.
Throws
InvalidKeySpecException if the given key specification is inappropriate for this key factory to produce a public key.

getAlgorithm

Added in API level 1
String getAlgorithm ()

获取与此 KeyFactory相关联的算法的名称。

Returns
String the name of the algorithm associated with this KeyFactory

getInstance

Added in API level 1
KeyFactory getInstance (String algorithm)

返回一个KeyFactory对象,用于转换指定算法的公钥/私钥。

该方法遍历注册安全提供程序的列表,从最优先的提供程序开始。 返回一个新的KeyFactory对象,该对象封装来自支持指定算法的第一个Provider的KeyFactorySpi实现。

请注意,注册供应商列表可能通过 Security.getProviders()方法检索。

Parameters
algorithm String: the name of the requested key algorithm. See the KeyFactory section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.
Returns
KeyFactory the new KeyFactory object.
Throws
NoSuchAlgorithmException if no Provider supports a KeyFactorySpi implementation for the specified algorithm.

也可以看看:

getInstance

Added in API level 1
KeyFactory getInstance (String algorithm, 
                String provider)

返回一个KeyFactory对象,用于转换指定算法的公钥/私钥。

返回封装指定提供者的KeyFactorySpi实现的新KeyFactory对象。 指定的提供者必须在安全提供者列表中注册。

请注意,注册供应商列表可以通过 Security.getProviders()方法检索。

Parameters
algorithm String: the name of the requested key algorithm. See the KeyFactory section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.
provider String: the name of the provider.
Returns
KeyFactory the new KeyFactory object.
Throws
NoSuchAlgorithmException if a KeyFactorySpi implementation for the specified algorithm is not available from the specified provider.
NoSuchProviderException if the specified provider is not registered in the security provider list.
IllegalArgumentException if the provider name is null or empty.

也可以看看:

getInstance

Added in API level 1
KeyFactory getInstance (String algorithm, 
                Provider provider)

返回一个KeyFactory对象,用于转换指定算法的公钥/私钥。

返回封装指定Provider对象的KeyFactorySpi实现的新KeyFactory对象。 请注意,指定的Provider对象不必在提供程序列表中注册。

Parameters
algorithm String: the name of the requested key algorithm. See the KeyFactory section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.
provider Provider: the provider.
Returns
KeyFactory the new KeyFactory object.
Throws
NoSuchAlgorithmException if a KeyFactorySpi implementation for the specified algorithm is not available from the specified Provider object.
IllegalArgumentException if the specified provider is null.

也可以看看:

getKeySpec

Added in API level 1
T getKeySpec (Key key, 
                Class<T> keySpec)

返回给定密钥对象的规范(密钥材料)。 keySpec标识应该返回密钥材料的规范类。 例如,它可以是DSAPublicKeySpec.class ,表示密钥材料应该在DSAPublicKeySpec类的实例中返回。

Parameters
key Key: the key.
keySpec Class: the specification class in which the key material should be returned.
Returns
T the underlying key specification (key material) in an instance of the requested specification class.
Throws
InvalidKeySpecException if the requested key specification is inappropriate for the given key, or the given key cannot be processed (e.g., the given key has an unrecognized algorithm or format).

getProvider

Added in API level 1
Provider getProvider ()

返回此关键工厂对象的提供者。

Returns
Provider the provider of this key factory object

translateKey

Added in API level 1
Key translateKey (Key key)

将提供者可能未知或可能不可信的密钥对象转换为此密钥工厂的相应密钥对象。

Parameters
key Key: the key whose provider is unknown or untrusted.
Returns
Key the translated key.
Throws
InvalidKeyException if the given key cannot be processed by this key factory.

Hooray!