在 Java 中生成 PKCS#1 格式的 RSA 密钥
当我使用 Java API 生成 RSA 密钥对时,公钥以 X.509 格式编码,私钥以 PKCS#8 格式编码。我希望将两者编码为 PKCS#1。这可能吗?我花了相当多的时间浏览 Java 文档,但还没有找到解决方案。当我使用 Java 和 Bouncy Castle 提供程序时,结果是相同的。
以下是代码片段:
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA","BC");
keygen.initialize(1024);
KeyPair pair = keygen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
byte[] privBytes = priv.getEncoded();
byte[] pubBytes = pub.getEncoded();
生成的两个字节数组的格式为 X.509(公共)和 PKCS#8(私有)。
任何帮助将不胜感激。有一些类似的帖子,但没有一个真正回答我的问题。
谢谢
When I generate an RSA key pair using the Java API, the public key is encoded in the X.509 format and the private key is encoded in the PKCS#8 format. I'm looking to encode both as PKCS#1. Is this possible? I've spent a considerable amount of time going through the Java docs but haven't found a solution. The result is the same when I use the Java and the Bouncy Castle providers.
Here is a snippet of the code:
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA","BC");
keygen.initialize(1024);
KeyPair pair = keygen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
byte[] privBytes = priv.getEncoded();
byte[] pubBytes = pub.getEncoded();
The two resulting byte arrays are formatted as X.509 (public) and PKCS#8 (private).
Any help would be much appreciated. There are some similar posts but none really answer my question.
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您将需要 BouncyCastle:
下面的代码片段已经过检查并发现可与 Bouncy Castle 1.52 配合使用。
私钥
将私钥从 PKCS8 转换为 PKCS1:
将 PKCS1 中的私钥转换为 PEM:
使用命令行 OpenSSL 检查密钥格式是否符合预期:
公钥
将公钥从 X.509 subjectPublicKeyInfo 转换为 PKCS1:
将 PKCS1 中的公钥转换为PEM:
使用命令行 OpenSSL 检查密钥格式是否符合预期:
谢谢
非常感谢以下帖子的作者:
这些帖子包含有用但不完整且有时过时的信息(即对于较旧的信息) BouncyCastle 的版本),这帮助我构建了这篇文章。
You will need BouncyCastle:
The code snippets below have been checked and found working with Bouncy Castle 1.52.
Private key
Convert private key from PKCS8 to PKCS1:
Convert private key in PKCS1 to PEM:
Check with command line OpenSSL that the key format is as expected:
Public key
Convert public key from X.509 SubjectPublicKeyInfo to PKCS1:
Convert public key in PKCS1 to PEM:
Check with command line OpenSSL that the key format is as expected:
Thanks
Many thanks to the authors of the following posts:
Those posts contained useful, but incomplete and sometimes outdated info (i.e. for older versions of BouncyCastle), that helped me to construct this post.
从 RFC5208 开始,PKCS#8 未加密格式由
PrivateKeyInfo 结构:
其中
privateKey
是:这个 RSAPrivateKey 结构只是密钥的 PKCS#1 编码,我们可以使用 BouncyCastle 提取它:
From RFC5208, the PKCS#8 unencrypted format consists of a
PrivateKeyInfo
structure:where
privateKey
is:This
RSAPrivateKey
structure is just the PKCS#1 encoding of the key, which we can extract using BouncyCastle:我写了一个C程序将pkcs8私钥转换为pkcs1。有用!
I wrote a C programme to convert pkcs8 private key to pkcs1. It works!
BouncyCastle 框架有一个 PKCS1 编码器来解决这个问题:http://www.bouncycastle。 org/docs/docs1.6/index.html
The BouncyCastle framework has a PKCS1 Encoder to solve this: http://www.bouncycastle.org/docs/docs1.6/index.html
我试图使用移植到 BlackBerry 的 BountyCastle J2ME 库生成 DER 格式的 OpenSSL 友好的 RSA 公钥,我的代码:
密钥仍然不正确:
我更改了 org.bouncycastle.asn1.x509.AlgorithmIdentifier
现在有了很好的密钥:
这可以是用于加密:
I was trying to generate OpenSSL-friendly RSA public keys in DER format using BountyCastle J2ME library ported to BlackBerry, my code:
Key was still incorrect:
I changed org.bouncycastle.asn1.x509.AlgorithmIdentifier
And now have nice key:
Which can be used to encrypt:
我知道这是旧帖子。但我花了两天时间解决这个问题,终于发现 BouncyCastle 可以做到这一点
http://www.bouncycastle.org/docs/ docs1.5on/org/bouncycastle/asn1/ASN1Encodable.html
I know this is old post. but I spent two days to solve this problem and finally find BouncyCastle can do that
http://www.bouncycastle.org/docs/docs1.5on/org/bouncycastle/asn1/ASN1Encodable.html