public class KeyGenerator
extends Object
| java.lang.Object | |
| ↳ | javax.crypto.KeyGenerator |
该类提供秘密(对称)密钥生成器的功能。
密钥生成器使用该类的 getInstance类方法之一构造。
KeyGenerator对象是可重用的,也就是说,在生成密钥后,可以重新使用相同的KeyGenerator对象来生成其他密钥。
有两种方法可以生成密钥:以独立于算法的方式,以特定于算法的方式。 两者之间的唯一区别是对象的初始化:
所有密钥生成器都共享密钥和随机源的概念。 在这个KeyGenerator类中有一个init方法,它接受这两个通用共享类型的参数。 还有一个只需要keysize参数,并使用最高优先级的安装提供程序的SecureRandom实现作为随机源(或者,如果没有安装的提供程序提供SecureRandom实现,则是系统提供的随机源),以及一个只需要一个随机性来源。
由于在调用上述算法独立的 init方法时没有指定其他参数,因此提供程序应该如何处理与每个密钥相关的算法特定参数(如果有)。
对于已经存在一组算法特定参数的情况,有两个init方法具有AlgorithmParameterSpec参数。 其中一个也有SecureRandom参数,而另一个使用最高优先级安装提供程序的SecureRandom实现作为随机源(或者,如果没有安装的提供程序提供SecureRandom实现,则是系统提供的随机源)。
如果客户端未明确初始化KeyGenerator(通过调用 init方法),则每个提供者都必须提供(并记录)默认初始化。
Android提供了以下 KeyGenerator算法:
| Name | Supported (API Levels) |
|---|---|
| AES | 1+ |
| AESWRAP | 1–8 |
| ARC4 | 14+ |
| Blowfish | 10+ |
| DES | 1+ |
| DESede | 1+ |
| DESedeWRAP | 1–8 |
| HmacMD5 | 1+ |
| HmacSHA1 | 1+ |
| HmacSHA224 | 1–8,22+ |
| HmacSHA256 | 1+ |
| HmacSHA384 | 1+ |
| HmacSHA512 | 1+ |
| RC4 | 10–13 |
也可以看看:
Protected constructors |
|
|---|---|
KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm) 创建KeyGenerator对象。 |
|
Public methods |
|
|---|---|
final SecretKey |
generateKey() 生成一个密钥。 |
final String |
getAlgorithm() 返回此 |
static final KeyGenerator |
getInstance(String algorithm) 返回一个为指定算法生成密钥的 |
static final KeyGenerator |
getInstance(String algorithm, String provider) 返回一个为指定算法生成密钥的 |
static final KeyGenerator |
getInstance(String algorithm, Provider provider) 返回一个为指定算法生成密钥的 |
final Provider |
getProvider() 返回此 |
final void |
init(AlgorithmParameterSpec params, SecureRandom random) 使用指定的参数集和用户提供的随机源初始化此密钥生成器。 |
final void |
init(SecureRandom random) 初始化此密钥生成器。 |
final void |
init(int keysize) 为特定的密钥大小初始化此密钥生成器。 |
final void |
init(int keysize, SecureRandom random) 使用用户提供的随机源初始化此密钥生成器的某个密钥大小。 |
final void |
init(AlgorithmParameterSpec params) 使用指定的参数集初始化此密钥生成器。 |
Inherited methods |
|
|---|---|
java.lang.Object
|
|
KeyGenerator (KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)
创建KeyGenerator对象。
| Parameters | |
|---|---|
keyGenSpi |
KeyGeneratorSpi: the delegate |
provider |
Provider: the provider |
algorithm |
String: the algorithm |
String getAlgorithm ()
返回此 KeyGenerator对象的算法名称。
这与在创建此 KeyGenerator对象的 getInstance调用之一中指定的名称相同。
| Returns | |
|---|---|
String |
the algorithm name of this KeyGenerator object. |
KeyGenerator getInstance (String algorithm)
返回一个为指定算法生成密钥的 KeyGenerator对象。
该方法遍历注册安全提供程序的列表,从最优先的提供程序开始。 返回封装来自支持指定算法的第一个Provider的KeyGeneratorSpi实现的新KeyGenerator对象。
请注意,注册供应商列表可能通过 Security.getProviders()方法检索。
| Parameters | |
|---|---|
algorithm |
String: the standard name of the requested key algorithm. See the KeyGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
| Returns | |
|---|---|
KeyGenerator |
the new KeyGenerator object. |
| Throws | |
|---|---|
NullPointerException |
if the specified algorithm is null. |
NoSuchAlgorithmException |
if no Provider supports a KeyGeneratorSpi implementation for the specified algorithm. |
也可以看看:
KeyGenerator getInstance (String algorithm, String provider)
返回一个为指定算法生成密钥的 KeyGenerator对象。
返回封装指定提供程序的KeyGeneratorSpi实现的新KeyGenerator对象。 指定的提供者必须在安全提供者列表中注册。
请注意,注册供应商列表可能通过 Security.getProviders()方法检索。
| Parameters | |
|---|---|
algorithm |
String: the standard name of the requested key algorithm. See the KeyGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
provider |
String: the name of the provider. |
| Returns | |
|---|---|
KeyGenerator |
the new KeyGenerator object. |
| Throws | |
|---|---|
NullPointerException |
if the specified algorithm is null. |
NoSuchAlgorithmException |
if a KeyGeneratorSpi 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 is null or empty. |
也可以看看:
KeyGenerator getInstance (String algorithm, Provider provider)
返回一个为指定算法生成密钥的 KeyGenerator对象。
返回封装指定Provider对象的KeyGeneratorSpi实现的新KeyGenerator对象。 请注意,指定的Provider对象不必在提供程序列表中注册。
| Parameters | |
|---|---|
algorithm |
String: the standard name of the requested key algorithm. See the KeyGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
provider |
Provider: the provider. |
| Returns | |
|---|---|
KeyGenerator |
the new KeyGenerator object. |
| Throws | |
|---|---|
NullPointerException |
if the specified algorithm is null. |
NoSuchAlgorithmException |
if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object. |
IllegalArgumentException |
if the provider is null. |
也可以看看:
Provider getProvider ()
返回此 KeyGenerator对象的提供者。
| Returns | |
|---|---|
Provider |
the provider of this KeyGenerator object |
void init (AlgorithmParameterSpec params, SecureRandom random)
使用指定的参数集和用户提供的随机源初始化此密钥生成器。
| Parameters | |
|---|---|
params |
AlgorithmParameterSpec: the key generation parameters |
random |
SecureRandom: the source of randomness for this key generator |
| Throws | |
|---|---|
InvalidAlgorithmParameterException |
if params is inappropriate for this key generator |
void init (SecureRandom random)
初始化此密钥生成器。
| Parameters | |
|---|---|
random |
SecureRandom: the source of randomness for this generator |
void init (int keysize)
为特定的密钥大小初始化此密钥生成器。
如果这个密钥生成器需要任何随机字节,它将使用最高优先级安装提供程序的实现作为随机源。 (如果没有安装的提供商提供SecureRandom的实现,则将使用系统提供的随机源。)SecureRandom
| Parameters | |
|---|---|
keysize |
int: the keysize. This is an algorithm-specific metric, specified in number of bits. |
| Throws | |
|---|---|
InvalidParameterException |
if the keysize is wrong or not supported. |
void init (int keysize,
SecureRandom random)
使用用户提供的随机源初始化此密钥生成器的某个密钥大小。
| Parameters | |
|---|---|
keysize |
int: the keysize. This is an algorithm-specific metric, specified in number of bits. |
random |
SecureRandom: the source of randomness for this key generator |
| Throws | |
|---|---|
InvalidParameterException |
if the keysize is wrong or not supported. |
void init (AlgorithmParameterSpec params)
使用指定的参数集初始化此密钥生成器。
如果这个密钥生成器需要任何随机字节,它将使用最高优先级安装提供程序的实现作为随机源。 (如果没有安装的提供商提供SecureRandom的实现,则将使用系统提供的随机源。)SecureRandom
| Parameters | |
|---|---|
params |
AlgorithmParameterSpec: the key generation parameters |
| Throws | |
|---|---|
InvalidAlgorithmParameterException |
if the given parameters are inappropriate for this key generator |