【发布时间】:2011-02-25 06:14:57
【问题描述】:
【问题讨论】:
-
看this的答案,我认为
PrivateKey.getEncoded()返回PKCS#8格式,PublicKey.getEncoded()返回X509格式。
【问题讨论】:
PrivateKey.getEncoded()返回PKCS#8格式,PublicKey.getEncoded()返回X509格式。
取决于密钥的类型。大多数对称密钥返回没有编码的原始字节。大多数公钥使用 ASN.1/DER 编码。
您不应该关心密钥的编码方式。将 getEncoded 视为序列化函数。它返回密钥的字节流表示,可以保存并稍后转换回密钥。
对于 RSA 私钥,它可能被编码为 PKCS#1 或 PKCS#8。 PKCS#1 是首选编码,因为它包含可加速私钥操作的额外 CRT 参数。
Sun JCE 始终以 PKCS#1 编码生成密钥对,因此私钥始终以 PKCS#1 中定义的这种格式编码,
--
-- Representation of RSA private key with information for the CRT algorithm.
--
RSAPrivateKey ::= SEQUENCE {
version Version,
modulus INTEGER, -- n
publicExponent INTEGER, -- e
privateExponent INTEGER, -- d
prime1 INTEGER, -- p
prime2 INTEGER, -- q
exponent1 INTEGER, -- d mod (p-1)
exponent2 INTEGER, -- d mod (q-1)
coefficient INTEGER, -- (inverse of q) mod p
otherPrimeInfos OtherPrimeInfos OPTIONAL
}
Version ::= INTEGER { two-prime(0), multi(1) }
(CONSTRAINED BY {-- version must be multi if otherPrimeInfos present --})
OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
OtherPrimeInfo ::= SEQUENCE {
prime INTEGER, -- ri
exponent INTEGER, -- di
coefficient INTEGER -- ti
}
【讨论】:
java.security.KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair().getPrivate()用的是哪个吗?