public abstract class KeyPairGenerator
extends KeyPairGeneratorSpi
| java.lang.Object | ||
| ↳ | java.security.KeyPairGeneratorSpi | |
| ↳ | java.security.KeyPairGenerator | |
KeyPairGenerator类用于生成公钥和私钥对。 密钥对生成器使用getInstance工厂方法(返回给定类的实例的静态方法)构造。
用于特定算法的密钥对生成器创建可用于该算法的公钥/私钥对。 它还将算法特定的参数与每个生成的密钥相关联。
有两种方法可以生成密钥对:以独立于算法的方式,以特定于算法的方式。 两者之间的唯一区别是对象的初始化:
所有密钥对生成器都共享密钥和随机源的概念。 对于不同的算法,密钥大小的解释是不同的(例如,在DSA算法的情况下,密钥大小对应于模数的长度)。 在这个KeyPairGenerator类中有一个initialize方法,它接受这两个通用共享类型的参数。 还有一个只需要keysize参数,并使用最高优先级的安装提供程序的SecureRandom实现作为随机源。 (如果没有安装的提供商提供SecureRandom的实现,则使用系统提供的随机源。)
由于在调用上述与算法无关的 initialize方法时没有指定其他参数,因此提供程序应该如何处理与每个密钥相关的算法特定参数(如果有)。
如果算法是DSA算法,并且密钥大小(模数大小)为p或1024,则Sun提供程序使用q和g参数的一组预计算值。 如果模数大小不是以上值之一,则Sun提供程序会创建一组新参数。 其他提供商可能有预先计算的参数集,不仅仅是上述三种模数大小。 还有一些可能根本没有预先计算的参数列表,而是总是创建新的参数集。
对于已经存在一组算法特定参数的情况(例如,DSA中所谓的社区参数 ),有两个initialize方法具有AlgorithmParameterSpec参数。 一个也有SecureRandom参数,而另一个使用最高优先级安装提供程序的SecureRandom实现作为随机源。 (如果没有安装的提供者提供SecureRandom的实现,则使用系统提供的随机源。)
如果客户端未明确初始化KeyPairGenerator(通过调用initialize方法),则每个提供者都必须提供(并记录)默认初始化。 例如, Sun提供程序使用1024位的默认模数大小(密钥大小)。
请注意,由于历史原因,此课程是抽象的,并且从KeyPairGeneratorSpi延伸。 应用程序开发人员只应该注意这个KeyPairGenerator类中定义的方法; 超类中的所有方法都用于希望提供自己的密钥对生成器实现的加密服务提供者。
Android提供了以下 KeyPairGenerator算法:
| Name | Supported (API Levels) |
|---|---|
| DH | 1+ |
| DSA | 1+ |
| EC | 11+ |
| RSA | 1+ |
也可以看看:
Protected constructors |
|
|---|---|
KeyPairGenerator(String algorithm) 为指定的算法创建一个KeyPairGenerator对象。 |
|
Public methods |
|
|---|---|
final KeyPair |
genKeyPair() 生成密钥对。 |
KeyPair |
generateKeyPair() 生成密钥对。 |
String |
getAlgorithm() 返回此密钥对生成器的算法的标准名称。 |
static KeyPairGenerator |
getInstance(String algorithm) 返回生成指定算法的公钥/私钥对的KeyPairGenerator对象。 |
static KeyPairGenerator |
getInstance(String algorithm, String provider) 返回生成指定算法的公钥/私钥对的KeyPairGenerator对象。 |
static KeyPairGenerator |
getInstance(String algorithm, Provider provider) 返回生成指定算法的公钥/私钥对的KeyPairGenerator对象。 |
final Provider |
getProvider() 返回此密钥对生成器对象的提供者。 |
void |
initialize(AlgorithmParameterSpec params, SecureRandom random) 使用给定的参数集和随机源初始化密钥对生成器。 |
void |
initialize(AlgorithmParameterSpec params) 使用指定参数集和最高优先级安装提供程序的 |
void |
initialize(int keysize, SecureRandom random) 使用给定的随机源(以及默认参数集)初始化某个密钥大小的密钥对生成器。 |
void |
initialize(int keysize) 使用默认参数集和最高优先级安装提供程序的 |
Inherited methods |
|
|---|---|
java.security.KeyPairGeneratorSpi
|
|
java.lang.Object
|
|
KeyPairGenerator (String algorithm)
为指定的算法创建一个KeyPairGenerator对象。
| Parameters | |
|---|---|
algorithm |
String: the standard string name of the algorithm. See the KeyPairGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
KeyPair genKeyPair ()
生成密钥对。
如果此KeyPairGenerator尚未显式初始化,则提供程序特定的缺省值将用于生成的密钥的大小和其他(算法特定的)值。
这将在每次调用时生成一个新的密钥对。
该方法在功能上等同于 generateKeyPair 。
| Returns | |
|---|---|
KeyPair |
the generated key pair |
KeyPair generateKeyPair ()
生成密钥对。
如果此KeyPairGenerator尚未显式初始化,则提供程序特定的缺省值将用于生成的密钥的大小和其他(算法特定的)值。
这将在每次调用时生成一个新的密钥对。
该方法在功能上等同于 genKeyPair 。
| Returns | |
|---|---|
KeyPair |
the generated key pair |
String getAlgorithm ()
返回此密钥对生成器的算法的标准名称。 有关标准算法名称的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的KeyPairGenerator部分。
| Returns | |
|---|---|
String |
the standard string name of the algorithm. |
KeyPairGenerator getInstance (String algorithm)
返回生成指定算法的公钥/私钥对的KeyPairGenerator对象。
该方法遍历注册安全提供程序的列表,从最优先的提供程序开始。 返回一个新的KeyPairGenerator对象,该对象封装来自支持指定算法的第一个Provider的KeyPairGeneratorSpi实现。
请注意,注册供应商列表可以通过 Security.getProviders()方法检索。
| Parameters | |
|---|---|
algorithm |
String: the standard string name of the algorithm. See the KeyPairGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
| Returns | |
|---|---|
KeyPairGenerator |
the new KeyPairGenerator object. |
| Throws | |
|---|---|
NoSuchAlgorithmException |
if no Provider supports a KeyPairGeneratorSpi implementation for the specified algorithm. |
也可以看看:
KeyPairGenerator getInstance (String algorithm, String provider)
返回生成指定算法的公钥/私钥对的KeyPairGenerator对象。
返回封装指定提供程序的KeyPairGeneratorSpi实现的新KeyPairGenerator对象。 指定的提供者必须在安全提供者列表中注册。
请注意,可以通过 Security.getProviders()方法检索已注册供应商的列表。
| Parameters | |
|---|---|
algorithm |
String: the standard string name of the algorithm. See the KeyPairGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
provider |
String: the string name of the provider. |
| Returns | |
|---|---|
KeyPairGenerator |
the new KeyPairGenerator object. |
| Throws | |
|---|---|
NoSuchAlgorithmException |
if a KeyPairGeneratorSpi 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. |
也可以看看:
KeyPairGenerator getInstance (String algorithm, Provider provider)
返回生成指定算法的公钥/私钥对的KeyPairGenerator对象。
返回封装指定Provider对象的KeyPairGeneratorSpi实现的新KeyPairGenerator对象。 请注意,指定的Provider对象不必在提供程序列表中注册。
| Parameters | |
|---|---|
algorithm |
String: the standard string name of the algorithm. See the KeyPairGenerator section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names. |
provider |
Provider: the provider. |
| Returns | |
|---|---|
KeyPairGenerator |
the new KeyPairGenerator object. |
| Throws | |
|---|---|
NoSuchAlgorithmException |
if a KeyPairGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object. |
IllegalArgumentException |
if the specified provider is null. |
也可以看看:
Provider getProvider ()
返回此密钥对生成器对象的提供者。
| Returns | |
|---|---|
Provider |
the provider of this key pair generator object |
void initialize (AlgorithmParameterSpec params, SecureRandom random)
使用给定的参数集和随机源初始化密钥对生成器。
这个具体的方法已被添加到这个以前定义的抽象类。 此方法调用KeyPairGeneratorSpi initialize方法,并将其传递给params和random 。 如果initialize方法没有被提供者覆盖,它总是抛出一个UnsupportedOperationException异常。
| Parameters | |
|---|---|
params |
AlgorithmParameterSpec: the parameter set used to generate the keys. |
random |
SecureRandom: the source of randomness. |
| Throws | |
|---|---|
InvalidAlgorithmParameterException |
if the given parameters are inappropriate for this key pair generator. |
void initialize (AlgorithmParameterSpec params)
使用指定的参数集和最高优先级安装提供程序的SecureRandom实现作为随机源初始化密钥对生成器。 (如果没有安装的提供者提供SecureRandom的实现,则使用系统提供的随机源。)。
这个具体的方法已被添加到这个以前定义的抽象类。 此方法调用KeyPairGeneratorSpi initialize方法,传递它params和一个随机性来源(从最高优先级的已安装提供程序或系统获得 - 如果没有安装的提供程序提供一个)。 如果initialize方法未被提供者覆盖,则该方法总是抛出UnsupportedOperationException。
| Parameters | |
|---|---|
params |
AlgorithmParameterSpec: the parameter set used to generate the keys. |
| Throws | |
|---|---|
InvalidAlgorithmParameterException |
if the given parameters are inappropriate for this key pair generator. |
void initialize (int keysize,
SecureRandom random)
使用给定的随机源(以及默认参数集)初始化某个密钥大小的密钥对生成器。
| Parameters | |
|---|---|
keysize |
int: the keysize. This is an algorithm-specific metric, such as modulus length, specified in number of bits. |
random |
SecureRandom: the source of randomness. |
| Throws | |
|---|---|
InvalidParameterException |
if the keysize is not supported by this KeyPairGenerator object. |
void initialize (int keysize)
初始化使用默认的参数设置和确定密钥大小的密钥对生成SecureRandom实现的最高优先级安装的提供作为随机源。 (如果没有安装的提供商提供SecureRandom的实现,则使用系统提供的随机源。)
| Parameters | |
|---|---|
keysize |
int: the keysize. This is an algorithm-specific metric, such as modulus length, specified in number of bits. |
| Throws | |
|---|---|
InvalidParameterException |
if the keysize is not supported by this KeyPairGenerator object. |