密钥对象和密钥规范(KeySpecs)是密钥数据的两种不同表示。
密码使用 Key 对象来初始化其加密算法,但可能需要将密钥转换为更便携的格式以进行传输或存储。
键的透明表示意味着您可以通过相应规范类中定义的一种 get 方法单独访问每个键材料值。
For example, DSAPrivateKeySpec defines getX, getP, getQ, and getG methods, to access the private key x, and the DSA algorithm parameters used to calculate the key(the prime p, the sub-prime q, and the base g).
如果密钥存储在硬件设备上,则其规范可能包含有助于识别设备上密钥的信息。这种表示与 Key 接口定义的不透明表示形成对比,在这种表示中您无法直接访问密钥材料领域。换句话说,“不透明”表示使您可以有限地访问密钥——只有 Key 接口定义的三个方法:getAlgorithm、getFormat 和 getEncoded。
可以以特定于算法的方式或以与算法无关的编码格式(例如 ASN.1)来指定密钥。
For example, a DSA private key may be specified by its components x, p, q, and g (eg: DSAPrivateKeySpec), or it may be specified using its DER encoding (eg: PKCS8EncodedKeySpec).
KeyFactory 和 SecretKeyFactory 类可用于在不透明和透明密钥表示之间进行转换,即在 Keys 和 KeySpecs 之间进行转换。
参考资料和更多详细信息,请访问:
https ://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html#KeySpecs