JavaTM 2 Platform
Standard Ed. 6

javax.crypto
类 KeyAgreement

java.lang.Object
  继承者 javax.crypto.KeyAgreement

public class KeyAgreement
extends Object

此类提供密钥协定(或密钥交换)协议的功能。

建立共享秘密所涉及的密钥由某个密钥生成器(KeyPairGeneratorKeyGenerator)或一个 KeyFactory 来创建,或者在密钥协定协议的中间阶段创建。

对于密钥交换中的每个相关方,需要调用 doPhase。例如,如果此密钥交换是与其他一个参与者进行的,则需要调用一次 doPhase,调用时将 lastPhase 标志设置为 true。如果此密钥交换是与其他两个参与者进行的,则需要调用两次 doPhase,第一次将 lastPhase 标志设置为 false,第二次将它设置为 true。密钥交换可以涉及任意数量的参与者。

从以下版本开始:
1.4
另请参见:
KeyGenerator, SecretKey

构造方法摘要
protected KeyAgreement(KeyAgreementSpi keyAgreeSpi, Provider provider, String algorithm)
          创建一个 KeyAgreement 对象。
 
方法摘要
 Key doPhase(Key key, boolean lastPhase)
          用给定密钥执行此密钥协定的下一个阶段,给定密钥是从此密钥协定所涉及的其他某个参与者那里接收的。
 byte[] generateSecret()
          生成共享秘密并在新的缓冲区中返回它。
 int generateSecret(byte[] sharedSecret, int offset)
          生成共享秘密,并将其放入缓冲区 sharedSecret,从 offset(包括)开始。
 SecretKey generateSecret(String algorithm)
          创建共享秘密并将其作为指定算法的 SecretKey 对象返回。
 String getAlgorithm()
          返回此 KeyAgreement 对象的算法名称。
static KeyAgreement getInstance(String algorithm)
          返回实现指定密钥协定算法的 KeyAgreement 对象。
static KeyAgreement getInstance(String algorithm, Provider provider)
          返回实现指定密钥协定算法的 KeyAgreement 对象。
static KeyAgreement getInstance(String algorithm, String provider)
          返回实现指定密钥协定算法的 KeyAgreement 对象。
 Provider getProvider()
          返回此 KeyAgreement 对象的提供者。
 void init(Key key)
          用给定密钥初始化此密钥协定,给定密钥需要包含此密钥协定所需的所有算法参数。
 void init(Key key, AlgorithmParameterSpec params)
          用给定密钥和算法参数集初始化此密钥协定。
 void init(Key key, AlgorithmParameterSpec params, SecureRandom random)
          用给定密钥、算法参数集和随机源初始化此密钥协定。
 void init(Key key, SecureRandom random)
          用给定密钥和随机源初始化此密钥协议。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

KeyAgreement

protected KeyAgreement(KeyAgreementSpi keyAgreeSpi,
                       Provider provider,
                       String algorithm)
创建一个 KeyAgreement 对象。

参数:
keyAgreeSpi - 委托
provider - 提供者
algorithm - 算法
方法详细信息

getAlgorithm

public final String getAlgorithm()
返回此 KeyAgreement 对象的算法名称。

此名称与在某个创建此 KeyAgreement 对象的 getInstance 调用中指定的名称相同。

返回:
KeyAgreement 对象的算法名称。

getInstance

public static final KeyAgreement getInstance(String algorithm)
                                      throws NoSuchAlgorithmException
返回实现指定密钥协定算法的 KeyAgreement 对象。

此方法从首选 Provider 开始遍历已注册安全提供者列表。返回一个封装 KeyAgreementSpi 实现的新 KeyAgreement 对象,该实现取自支持指定算法的第一个 Provider。

注意,可以通过 Security.getProviders() 方法获取已注册提供者列表。

参数:
algorithm - 所请求密钥协定算法的标准名称。有关标准算法名称的信息,请参阅 Java Cryptography Architecture Reference Guide 中的附录 A。
返回:
新的 KeyAgreement 对象。
抛出:
NullPointerException - 如果指定的算法为 null。
NoSuchAlgorithmException - 如果没有 Provider 支持指定算法的 KeyAgreementSpi 实现。
另请参见:
Provider

getInstance

public static final KeyAgreement getInstance(String algorithm,
                                             String provider)
                                      throws NoSuchAlgorithmException,
                                             NoSuchProviderException
返回实现指定密钥协定算法的 KeyAgreement 对象。

返回一个封装 KeyAgreementSpi 实现的新 KeyAgreement 对象,该实现取自指定的提供者。指定提供者必须在安全提供者列表中注册。

注意,可以通过 Security.getProviders() 方法获取已注册提供者列表。

参数:
algorithm - 所请求密钥协定算法的标准名称。有关标准算法名称的信息,请参阅 Java Cryptography Architecture Reference Guide 中的附录 A。
provider - 提供者的名称。
返回:
新的 KeyAgreement 对象。
抛出:
NullPointerException - 如果指定的算法为 null。
NoSuchAlgorithmException - 如果不能从指定提供者获得指定算法的 KeyAgreementSpi 实现。
NoSuchProviderException - 如果指定提供者未在安全提供者列表中注册。
IllegalArgumentException - 如果 provider 为 null 或空。
另请参见:
Provider

getInstance

public static final KeyAgreement getInstance(String algorithm,
                                             Provider provider)
                                      throws NoSuchAlgorithmException
返回实现指定密钥协定算法的 KeyAgreement 对象。

返回一个封装 KeyAgreementSpi 实现的新 KeyAgreement 对象,该实现取自指定的 Provider 对象。 注意,指定的 Provider 对象无需在提供者列表中注册。

参数:
algorithm - 所请求密钥协定算法的标准名称。有关标准算法名称的信息,请参阅 Java Cryptography Architecture Reference Guide 中的附录 A。
provider - 提供者。
返回:
新的 KeyAgreement 对象。
抛出:
NullPointerException - 如果指定的算法为 null。
NoSuchAlgorithmException - 如果不能从指定的 Provider 对象获得指定算法的 KeyAgreementSpi 实现。
IllegalArgumentException - 如果 provider 为 null。
另请参见:
Provider

getProvider

public final Provider getProvider()
返回此 KeyAgreement 对象的提供者。

返回:
KeyAgreement 对象的提供者

init

public final void init(Key key)
                throws InvalidKeyException
用给定密钥初始化此密钥协定,给定密钥需要包含此密钥协定所需的所有算法参数。

如果此密钥协定需要随机字节,则它将使用具有高优先级的已安装提供者的 SecureRandom 实现获取它们以作为随机源。(如果已安装的提供者没有一个提供 SecureRandom 实现,则将使用系统提供的随机源。)

参数:
key - 参与者的私有信息。例如,对于 Diffie-Hellman 密钥协定,此参数将是参与者自己的 Diffie-Hellman 私钥。
抛出:
InvalidKeyException - 如果给定的密钥不适合此密钥协定,例如,属于错误类型或算法类型不一致。

init

public final void init(Key key,
                       SecureRandom random)
                throws InvalidKeyException
用给定密钥和随机源初始化此密钥协议。给定密钥需要包含此密钥协议所需的所有算法参数。

如果该密钥协定算法需要随机字节,则从给定的随机源 random 中获取它们。但是,如果底层算法实现不需要任何随机字节,则忽略 random

参数:
key - 参与者的私有信息。例如,对于 Diffie-Hellman 密钥协定,此参数将是参与者自己的 Diffie-Hellman 私钥。
random - 随机源
抛出:
InvalidKeyException - 如果给定的密钥不适合此密钥协定,例如,属于错误类型或算法类型不一致。

init

public final void init(Key key,
                       AlgorithmParameterSpec params)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
用给定密钥和算法参数集初始化此密钥协定。

如果此密钥协定需要随机字节,则它将使用具有高优先级的已安装提供者的 SecureRandom 实现获取它们以作为随机源。(如果已安装的提供者没有一个提供 SecureRandom 实现,则将使用系统提供的随机源。)

参数:
key - 参与者的私有信息。例如,对于 Diffie-Hellman 密钥协定,此参数将是参与者自己的 Diffie-Hellman 私钥。
params - 密钥协定参数
抛出:
InvalidKeyException - 如果给定的密钥不适合此密钥协定,例如,属于错误类型或算法类型不一致。
InvalidAlgorithmParameterException - 如果给定算法不适合此密钥协定。

init

public final void init(Key key,
                       AlgorithmParameterSpec params,
                       SecureRandom random)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
用给定密钥、算法参数集和随机源初始化此密钥协定。

参数:
key - 参与者的私有信息。例如,对于 Diffie-Hellman 密钥协定,此参数将是参与者自己的 Diffie-Hellman 私钥。
params - 密钥协定参数
random - 随机源
抛出:
InvalidKeyException - 如果给定的密钥不适合此密钥协定,例如,属于错误类型或算法类型不一致。
InvalidAlgorithmParameterException - 如果给定算法不适合此密钥协定。

doPhase

public final Key doPhase(Key key,
                         boolean lastPhase)
                  throws InvalidKeyException,
                         IllegalStateException
用给定密钥执行此密钥协定的下一个阶段,给定密钥是从此密钥协定所涉及的其他某个参与者那里接收的。

参数:
key - 此阶段的密钥。例如,对于两个参与者之间的 Diffie-Hellman,此密钥将是另一个参与者的 Diffie-Hellman 公钥。
lastPhase - 表示这是否是此密钥协定最后阶段的标志。
返回:
从此阶段得到的(中间)密钥;如果此阶段不产生密钥,则返回 null
抛出:
InvalidKeyException - 如果给定密钥不适合此阶段。
IllegalStateException - 如果尚未初始化此密钥协定。

generateSecret

public final byte[] generateSecret()
                            throws IllegalStateException
生成共享秘密并在新的缓冲区中返回它。

此方法重置此 KeyAgreement 对象,以便能够将它重用于进一步的密钥协定。如果没有使用某个 init 方法重新初始化此密钥协定,则将相同的私有信息和算法参数用于后续密钥协定。

返回:
包含共享秘密的新缓冲区
抛出:
IllegalStateException - 如果尚未完成此密钥协定

generateSecret

public final int generateSecret(byte[] sharedSecret,
                                int offset)
                         throws IllegalStateException,
                                ShortBufferException
生成共享秘密,并将其放入缓冲区 sharedSecret,从 offset(包括)开始。

如果 sharedSecret 缓冲区太小而无法保存该结果,则抛出 ShortBufferException。在此情况下,应该用更大的输出缓冲区再次调用此方法。

此方法重置此 KeyAgreement 对象,以便能够将它重用于进一步的密钥协定。如果没有使用某个 init 方法重新初始化此密钥协定,则将相同的私有信息和算法参数用于后续密钥协定。

参数:
sharedSecret - 共享秘密的缓冲区
offset - sharedSecret 中将存储共享秘密处的偏移量
返回:
放入 sharedSecret 中的字节数
抛出:
IllegalStateException - 如果尚未完成此密钥协定
ShortBufferException - 如果给定输出缓冲区太小而无法保存该秘密

generateSecret

public final SecretKey generateSecret(String algorithm)
                               throws IllegalStateException,
                                      NoSuchAlgorithmException,
                                      InvalidKeyException
创建共享秘密并将其作为指定算法的 SecretKey 对象返回。

此方法重置此 KeyAgreement 对象,以便能够将它重用于进一步的密钥协定。如果没有使用某个 init 方法重新初始化此密钥协定,则将相同的私有信息和算法参数用于后续密钥协定。

参数:
algorithm - 所请求秘密密钥的算法
返回:
共享的秘密密钥
抛出:
IllegalStateException - 如果尚未完成此密钥协定
NoSuchAlgorithmException - 如果指定的秘密密钥算法不可用
InvalidKeyException - 如果无法使用共享秘密密钥材料生成指定算法的秘密密钥(例如,密钥材料太短)

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策