public interface KEMSpi
该类定义了
KEM类的服务提供者接口(SPI)。安全提供者实现此接口以提供密钥封装机制(KEM)算法的实现。
KEM算法可能支持一组配置。每个配置可能接受不同类型的密钥、加密原语以及共享密钥和密钥封装消息的大小。配置由KEM算法名称、它使用的密钥以及在创建封装器或解封装器时指定的可选AlgorithmParameterSpec参数定义。调用engineNewEncapsulator(java.security.PublicKey, java.security.spec.AlgorithmParameterSpec, java.security.SecureRandom)或engineNewDecapsulator(java.security.PrivateKey, java.security.spec.AlgorithmParameterSpec)的结果必须返回一个映射到单个配置的封装器或解封装器,其中其engineSecretSize()和engineEncapsulationSize()方法返回常量值。
KEMSpi实现必须是不可变的。同时调用多个engineNewEncapsulator和engineNewDecapsulator方法必须是安全的。
EncapsulatorSpi和DecapsulatorSpi实现也必须是不可变的。同时调用多个encapsulate和decapsulate方法必须是安全的。每次调用encapsulate应生成一个新的共享密钥和密钥封装消息。
例如,
public static class MyKEMImpl implements KEMSpi {
@Override
public KEMSpi.EncapsulatorSpi engineNewEncapsulator(PublicKey publicKey,
AlgorithmParameterSpec spec, SecureRandom secureRandom)
throws InvalidAlgorithmParameterException, InvalidKeyException {
if (!checkPublicKey(publicKey)) {
throw new InvalidKeyException("不支持的密钥");
}
if (!checkParameters(spec)) {
throw new InvalidAlgorithmParameterException("不支持的参数");
}
return new MyEncapsulator(publicKey, spec, secureRandom);
}
class MyEncapsulator implements KEMSpi.EncapsulatorSpi {
MyEncapsulator(PublicKey publicKey, AlgorithmParameterSpec spec,
SecureRandom secureRandom){
this.spec = spec != null ? spec : getDefaultParameters();
this.secureRandom = secureRandom != null
? secureRandom
: getDefaultSecureRandom();
this.publicKey = publicKey;
}
@Override
public KEM.Encapsulated encapsulate(int from, int to, String algorithm) {
byte[] encapsulation;
byte[] secret;
// 计算...
return new KEM.Encapsulated(
new SecretKeySpec(secret, from, to - from, algorithm),
encapsulation, null);
}
// ...
}
// ...
}
- 自:
- 21
- 参见:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface由KEM接收方生成的KEM解封装器实现,由engineNewDecapsulator(java.security.PrivateKey, java.security.spec.AlgorithmParameterSpec)生成。static interface -
Method Summary
Modifier and TypeMethodDescriptionengineNewDecapsulator(PrivateKey privateKey, AlgorithmParameterSpec spec) 在KEM接收方创建KEM解封装器。engineNewEncapsulator(PublicKey publicKey, AlgorithmParameterSpec spec, SecureRandom secureRandom) 在KEM发送方创建KEM封装器。
-
Method Details
-
engineNewEncapsulator
KEMSpi.EncapsulatorSpi engineNewEncapsulator(PublicKey publicKey, AlgorithmParameterSpec spec, SecureRandom secureRandom) throws InvalidAlgorithmParameterException, InvalidKeyException 在KEM发送方创建KEM封装器。- 参数:
-
publicKey- 接收方的公钥,不得为null -
spec- 可选参数,可以为null -
secureRandom- 用于封装的随机源。如果为null,则实现必须提供默认值。 - 返回:
- 此密钥的封装器
- 抛出:
-
InvalidAlgorithmParameterException- 如果spec无效或需要一个但spec为null -
InvalidKeyException- 如果publicKey为null或无效 - 参见:
-
engineNewDecapsulator
KEMSpi.DecapsulatorSpi engineNewDecapsulator(PrivateKey privateKey, AlgorithmParameterSpec spec) throws InvalidAlgorithmParameterException, InvalidKeyException 在KEM接收方创建KEM解封装器。- 参数:
-
privateKey- 接收方的私钥,不得为null -
spec- 可选参数,可以为null - 返回:
- 此密钥的解封装器
- 抛出:
-
InvalidAlgorithmParameterException- 如果spec无效或需要一个但spec为null -
InvalidKeyException- 如果privateKey为null或无效 - 参见:
-