Most visited

Recently visited

Added in API level 1

KeyAgreementSpi

public abstract class KeyAgreementSpi
extends Object

java.lang.Object
   ↳ javax.crypto.KeyAgreementSpi


该类定义了KeyAgreement类的 ( SPI )。 这个类中的所有抽象方法都必须由希望提供特定密钥协议算法实现的每个加密服务提供者来实现。

建立共享密钥所涉及的密钥由密钥生成器之一( KeyPairGeneratorKeyGenerator ), KeyFactory或密钥协议协议( engineDoPhase )的中间阶段的结果 engineDoPhase

对于密钥交换中的每个记者,需要调用engineDoPhase 例如,如果密钥交换是与另一方进行的, engineDoPhase需要调用lastPhase一次,并将lastPhase标志设置为true 如果密钥交换是与另外两方进行的, engineDoPhase需要调用engineDoPhase两次,第一次将lastPhase标志设置为false ,第二次将其设置为true 密钥交换中可能有多个参与方。

也可以看看:

Summary

Public constructors

KeyAgreementSpi()

Protected methods

abstract Key engineDoPhase(Key key, boolean lastPhase)

使用从此密钥协议中涉及的其他参与方之一收到的给定密钥执行此密钥协议的下一阶段。

abstract byte[] engineGenerateSecret()

生成共享密钥并将其返回到新缓冲区中。

abstract SecretKey engineGenerateSecret(String algorithm)

创建共享密钥并将其作为请求算法类型的密钥对象返回。

abstract int engineGenerateSecret(byte[] sharedSecret, int offset)

生成共享密钥,并将其放入缓冲区 sharedSecret ,从 offset开始(包括 offset

abstract void engineInit(Key key, SecureRandom random)

使用给定的密钥和随机源初始化此密钥协议。

abstract void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random)

使用给定密钥,算法参数集和随机源初始化此密钥协议。

Inherited methods

From class java.lang.Object

Public constructors

KeyAgreementSpi

Added in API level 1
KeyAgreementSpi ()

Protected methods

engineDoPhase

Added in API level 1
Key engineDoPhase (Key key, 
                boolean lastPhase)

使用从此密钥协议中涉及的其他参与方之一收到的给定密钥执行此密钥协议的下一阶段。

Parameters
key Key: the key for this phase. For example, in the case of Diffie-Hellman between 2 parties, this would be the other party's Diffie-Hellman public key.
lastPhase boolean: flag which indicates whether or not this is the last phase of this key agreement.
Returns
Key the (intermediate) key resulting from this phase, or null if this phase does not yield a key
Throws
InvalidKeyException if the given key is inappropriate for this phase.
IllegalStateException if this key agreement has not been initialized.

engineGenerateSecret

Added in API level 1
byte[] engineGenerateSecret ()

生成共享密钥并将其返回到新缓冲区中。

此方法重置此KeyAgreementSpi对象,以便它可以重新用于其他密钥协议。 除非使用engineInit方法之一重新初始化此密钥协议,否则相同的私有信息和算法参数将用于随后的密钥协议。

Returns
byte[] the new buffer with the shared secret
Throws
IllegalStateException if this key agreement has not been completed yet

engineGenerateSecret

Added in API level 1
SecretKey engineGenerateSecret (String algorithm)

创建共享密钥并将其作为请求算法类型的密钥对象返回。

此方法重置此KeyAgreementSpi对象,以便它可以重新用于其他密钥协议。 除非使用engineInit方法之一重新初始化此密钥协议,否则相同的私有信息和算法参数将用于随后的密钥协议。

Parameters
algorithm String: the requested secret key algorithm
Returns
SecretKey the shared secret key
Throws
IllegalStateException if this key agreement has not been completed yet
NoSuchAlgorithmException if the requested secret key algorithm is not available
InvalidKeyException if the shared secret key material cannot be used to generate a secret key of the requested algorithm type (e.g., the key material is too short)

engineGenerateSecret

Added in API level 1
int engineGenerateSecret (byte[] sharedSecret, 
                int offset)

生成共享密钥,并将其放入缓冲区 sharedSecret ,从 offset开始(包括 offset

如果sharedSecret缓冲区太小而无法保存结果, ShortBufferException引发ShortBufferException 在这种情况下,应该使用更大的输出缓冲区重复此调用。

此方法重置此KeyAgreementSpi对象,以便它可以重新用于其他密钥协议。 除非使用engineInit方法之一重新初始化此密钥协议,否则相同的私有信息和算法参数将用于随后的密钥协议。

Parameters
sharedSecret byte: the buffer for the shared secret
offset int: the offset in sharedSecret where the shared secret will be stored
Returns
int the number of bytes placed into sharedSecret
Throws
IllegalStateException if this key agreement has not been completed yet
ShortBufferException if the given output buffer is too small to hold the secret

engineInit

Added in API level 1
void engineInit (Key key, 
                SecureRandom random)

使用给定的密钥和随机源初始化此密钥协议。 给定密钥需要包含此密钥协议所需的所有算法参数。

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

Parameters
key Key: the party's private information. For example, in the case of the Diffie-Hellman key agreement, this would be the party's own Diffie-Hellman private key.
random SecureRandom: the source of randomness
Throws
InvalidKeyException if the given key is inappropriate for this key agreement, e.g., is of the wrong type or has an incompatible algorithm type.

engineInit

Added in API level 1
void engineInit (Key key, 
                AlgorithmParameterSpec params, 
                SecureRandom random)

使用给定密钥,算法参数集和随机源初始化此密钥协议。

Parameters
key Key: the party's private information. For example, in the case of the Diffie-Hellman key agreement, this would be the party's own Diffie-Hellman private key.
params AlgorithmParameterSpec: the key agreement parameters
random SecureRandom: the source of randomness
Throws
InvalidKeyException if the given key is inappropriate for this key agreement, e.g., is of the wrong type or has an incompatible algorithm type.
InvalidAlgorithmParameterException if the given parameters are inappropriate for this key agreement.

Hooray!