加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Generating RSA keys in PKCS#1 format in Java--转

发布时间:2020-12-14 06:23:19 所属栏目:Java 来源:网络整理
导读:原文地址:https://stackoverflow.com/questions/7611383/generating-rsa-keys-in-pkcs1-format-in-java 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

原文地址:https://stackoverflow.com/questions/7611383/generating-rsa-keys-in-pkcs1-format-in-java

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:

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

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:

<span class="typ">PrivateKeyInfo<span class="pln"> pkInfo <span class="pun">=<span class="pln"> <span class="typ">PrivateKeyInfo<span class="pun">.<span class="pln">getInstance<span class="pun">(<span class="pln">privBytes<span class="pun">);<span class="pln">
ASN1Encodable encodable <span class="pun">=<span class="pln"> pkInfo<span class="pun">.<span class="pln">parsePrivateKey<span class="pun">();<span class="pln">
ASN1Primitive primitive <span class="pun">=<span class="pln"> encodable<span class="pun">.<span class="pln">toASN1Primitive<span class="pun">();<span class="pln">
<span class="kwd">byte<span class="pun">[]<span class="pln"> privateKeyPKCS1 <span class="pun">=<span class="pln"> primitive<span class="pun">.<span class="pln">getEncoded<span class="pun">();

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:

<span class="typ">SubjectPublicKeyInfo<span class="pln"> spkInfo <span class="pun">=<span class="pln"> <span class="typ">SubjectPublicKeyInfo<span class="pun">.<span class="pln">getInstance<span class="pun">(<span class="pln">pubBytes<span class="pun">);<span class="pln">
ASN1Primitive primitive <span class="pun">=<span class="pln"> spkInfo<span class="pun">.<span class="pln">parsePublicKey<span class="pun">();<span class="pln">
<span class="kwd">byte<span class="pun">[]<span class="pln"> publicKeyPKCS1 <span class="pun">=<span class="pln"> primitive<span class="pun">.<span class="pln">getEncoded<span class="pun">();

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,though sometimes outdated info (i.e. for older versions of BouncyCastle),that helped me to construct this post.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读