- java.lang.Object
-
- javax.crypto.SecretKeyFactory
-
public class SecretKeyFactory extends Object
此类表示密钥的工厂。关键工厂用于将密钥 (类型为
Key不透明加密密钥)转换为关键规范 (底层密钥材料的透明表示),反之亦然。 密钥工厂仅在秘密(对称)密钥上运行。关键工厂是双向的,即,它们允许从给定的密钥规范(密钥材料)构建不透明的密钥对象,或者以合适的格式检索密钥对象的基础密钥材料。
应用程序开发人员应参考其提供商的文档,以了解
generateSecret和getKeySpec方法支持的关键规范。 例如,由“SunJCE”提供商提供的DES密钥工厂支持DESKeySpec作为DES密钥的透明表示,并且该提供商的Triple DES密钥的密钥工厂支持DESedeKeySpec作为Triple DES密钥的透明表示。需要Java平台的每个实现来支持以下标准
SecretKeyFactory算法:-
DES -
DESede
- 从以下版本开始:
- 1.4
- 另请参见:
-
SecretKey,DESKeySpec,DESedeKeySpec,PBEKeySpec
-
-
-
构造方法摘要
构造方法 变量 构造器 描述 protectedSecretKeyFactory(SecretKeyFactorySpi keyFacSpi, Provider provider, String algorithm)创建一个SecretKeyFactory对象。
-
方法摘要
所有方法 静态方法 实例方法 具体的方法 变量和类型 方法 描述 SecretKeygenerateSecret(KeySpec keySpec)从提供的密钥规范(密钥材料)生成SecretKey对象。StringgetAlgorithm()返回此SecretKeyFactory对象的算法名称。static SecretKeyFactorygetInstance(String algorithm)返回转换指定算法的密钥的SecretKeyFactory对象。static SecretKeyFactorygetInstance(String algorithm, String provider)返回转换指定算法的密钥的SecretKeyFactory对象。static SecretKeyFactorygetInstance(String algorithm, Provider provider)返回转换指定算法的密钥的SecretKeyFactory对象。KeySpecgetKeySpec(SecretKey key, 类<?> keySpec)以请求的格式返回给定密钥对象的规范(密钥材料)。ProvidergetProvider()返回此SecretKeyFactory对象的提供者。SecretKeytranslateKey(SecretKey key)将提供者可能未知或可能不受信任的密钥对象转换为此密钥工厂的相应密钥对象。
-
-
-
构造方法详细信息
-
SecretKeyFactory
protected SecretKeyFactory(SecretKeyFactorySpi keyFacSpi, Provider provider, String algorithm)
创建一个SecretKeyFactory对象。- 参数
-
keyFacSpi- 代表 -
provider- 提供者 -
algorithm- 秘密密钥算法
-
-
方法详细信息
-
getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
返回一个SecretKeyFactory对象,该对象转换指定算法的密钥。此方法遍历已注册的安全提供程序列表,从最首选的提供程序开始。 将返回一个新的SecretKeyFactory对象,该对象封装了第一个支持指定算法的Provider的SecretKeyFactorySpi实现。
请注意,可以通过
Security.getProviders()方法检索已注册提供程序的列表。- Implementation Note:
-
JDK Reference Implementation还使用
jdk.security.provider.preferredSecurity属性来确定指定算法的首选提供程序顺序。 这可能与Security.getProviders()返回的提供商的顺序不同。 - 参数
-
algorithm- 请求的密钥算法的标准名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的SecretKeyFactory部分。 - 结果
-
新的
SecretKeyFactory对象 - 异常
-
NoSuchAlgorithmException- 如果没有Provider支持指定算法的SecretKeyFactorySpi实现 -
NullPointerException- 如果algorithm是null - 另请参见:
-
Provider
-
getInstance
public static final SecretKeyFactory getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
返回一个SecretKeyFactory对象,该对象转换指定算法的密钥。将返回一个新的SecretKeyFactory对象,该对象封装了指定提供程序中的SecretKeyFactorySpi实现。 必须在安全提供程序列表中注册指定的提供程序。
请注意,可以通过
Security.getProviders()方法检索已注册提供商的列表。- 参数
-
algorithm- 请求的密钥算法的标准名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的SecretKeyFactory部分。 -
provider- 提供者的名称。 - 结果
-
新的
SecretKeyFactory对象 - 异常
-
IllegalArgumentException- 如果provider是null或为空 -
NoSuchAlgorithmException- 如果指定提供程序的指定算法的SecretKeyFactorySpi实现不可用 -
NoSuchProviderException- 如果指定的提供程序未在安全提供程序列表中注册 -
NullPointerException- 如果algorithm是null - 另请参见:
-
Provider
-
getInstance
public static final SecretKeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
返回一个SecretKeyFactory对象,该对象转换指定算法的密钥。将返回一个新的SecretKeyFactory对象,该对象封装了指定Provider对象的SecretKeyFactorySpi实现。 请注意,指定的Provider对象不必在提供程序列表中注册。
- 参数
-
algorithm- 请求的密钥算法的标准名称。 有关标准算法名称的信息,请参阅Java Security Standard Algorithm Names Specification中的SecretKeyFactory部分。 -
provider- 提供者。 - 结果
-
新的
SecretKeyFactory对象 - 异常
-
IllegalArgumentException- 如果provider是null -
NoSuchAlgorithmException- 如果指定的算法的SecretKeyFactorySpi实现不可用于指定的Provider对象 -
NullPointerException- 如果algorithm是null - 另请参见:
-
Provider
-
getProvider
public final Provider getProvider()
返回此SecretKeyFactory对象的提供者。- 结果
-
此
SecretKeyFactory对象的提供者
-
getAlgorithm
public final String getAlgorithm()
返回此SecretKeyFactory对象的算法名称。这与在创建此
SecretKeyFactory对象的getInstance调用之一中指定的名称相同。- 结果
-
此
SecretKeyFactory对象的算法名称。
-
generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
从提供的密钥规范(密钥材料)生成SecretKey对象。- 参数
-
keySpec- 密钥的规范(密钥材料) - 结果
- 秘密密钥
- 异常
-
InvalidKeySpecException- 如果给定的密钥规范不适合此密钥工厂生成密钥。
-
getKeySpec
public final KeySpec getKeySpec(SecretKey key, 类<?> keySpec) throws InvalidKeySpecException
以请求的格式返回给定密钥对象的规范(密钥材料)。- 参数
-
key- 关键 -
keySpec- 要求返回密钥材料的格式 - 结果
- 请求格式的基础密钥规范(密钥材料)
- 异常
-
InvalidKeySpecException- 如果请求的密钥规范不适合给定密钥(例如,与key和keySpec相关联的算法不匹配,或者key引用加密硬件设备上的密钥,而keySpec是基于软件的密钥的规范),或者不能处理给定的密钥(例如,给定密钥具有该秘密密钥工厂不支持的算法或格式)。
-
translateKey
public final SecretKey translateKey(SecretKey key) throws InvalidKeyException
将提供者可能未知或可能不受信任的密钥对象转换为此密钥工厂的相应密钥对象。- 参数
-
key- 提供者未知或不可信的密钥 - 结果
- 翻译的密钥
- 异常
-
InvalidKeyException- 如果此密钥工厂无法处理给定密钥。
-
-