java - java.security.Key.getEncoded() 是否以 DER 编码格式返回数据?

标签 java encoding rsa der

java.security.Key.getEncoded() 返回 DER 中的数据编码格式?

如果没有,有什么方法可以吗?

更新:包含 RSA 私钥实现的 key 接口(interface)

最佳答案

取决于 key 的类型。大多数对称 key 返回没有编码的原始字节。大多数公钥使用 ASN.1/DER 编码。

您不应该关心 key 的编码方式。将 getEncoded 视为序列化函数。它返回 key 的字节流表示形式,可以将其保存并稍后转换回 key 。

对于 RSA 私钥,它可能被编码为 PKCS#1 或 PKCS#8。 PKCS#1 是首选编码,因为它包含额外的 CRT 参数,可加速私钥操作。

Sun JCE 始终以 PKCS#1 编码生成 key 对,因此私钥始终以 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 - java.security.Key.getEncoded() 是否以 DER 编码格式返回数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2931390/

相关文章:

java - 如何从 java 应用程序连接到 PCF 并列出组织、空间、应用程序及其配置?

java - 通过双击在命令提示符下运行 .jar 文件

c++ - 将字符串序列化为 UTF 无 BOM

linux - Base64 是否有不同的编码约定?

java - 如何使用 save 方法更新 Spring Boot 中的元素?

java - LinkedHashMap<double[], Integer>,无法使用 .get 或 .containsKey 访问 Integer

Mysql 的 MD5 不能很好地处理编码

java - Algid 解析错误,不是序列

c++ - 如何使用 OpenSSL C 库将 RSA key 从二进制数据加载到 RSA 结构?

c - OpenMP 中的并行 RSA 加密