java rsa 转pem,将RSA密钥转换为PEM格式

I have an RSA keypair in decimal format like this:

N: 131380300130444423689465024460852313971098730922811994958210650530501686748132880102503190365296216968351535889369502651601697016994057094307459860310817213533755054007252477133258682280599098830508996183566745393684789271087614478241425320061726198137426426490142200235611844869472546908487777450913733956847

E: 65537

D: 80297204963989065579466889768824319637950465647088430322583084471355799970954766200860052948440422519604509972209521777162610037317804551817832746460011635684494412969232268215156141089492528372187753214602862715747188949826914338588271329247689858629892142371556575928454002581316958535707202236560574870513

P: 1224584489781086541356110467036625215751324632060784958572680296867401248241071907258999049522896850209985495153134079568147009917335592949603533318035603

Q: 10728561502026755927334064184961854822182870744919733441933716834397978727498603128211162084788541605856166966893670172798846339557261092973389679407794073

While OpenSSL (for PHP) requires that the key should be in PEM format. How do i convert them? It is very important that the public & private key "stays the same" (By that i mean that i can't just make a new keypair).

EDIT:

Or is it easier to make a pem cartificate and extracting all of those values in decimal format? And if so, how?

解决方案

Short of doing it yourself (writing a small utitlity program to convert the key to PEM format) you could use the Java tool at this URL: http://www.platanus.cz/blog/converting-rsa-xml-key-to-pem

It requires the input to be in an XML file but that should be trivial for you to generate.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以通过以下方式将RSA密钥转换为不同的格式: 1. 将RSA密钥转换为PKCS#8格式的私钥: ``` PrivateKey privateKey = ... // 获取RSA私钥 byte[] pkcs8Bytes = privateKey.getEncoded(); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(pkcs8Bytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey newPrivateKey = keyFactory.generatePrivate(pkcs8KeySpec); ``` 2. 将RSA密钥转换为PKCS#1格式的私钥: ``` PrivateKey privateKey = ... // 获取RSA私钥 byte[] pkcs1Bytes = privateKey.getEncoded(); PKCS1EncodedKeySpec pkcs1KeySpec = new PKCS1EncodedKeySpec(pkcs1Bytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey newPrivateKey = keyFactory.generatePrivate(pkcs1KeySpec); ``` 3. 将RSA密钥转换为X.509格式的公钥: ``` PublicKey publicKey = ... // 获取RSA公钥 byte[] x509Bytes = publicKey.getEncoded(); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(x509Bytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey newPublicKey = keyFactory.generatePublic(x509KeySpec); ``` 4. 将RSA密钥转换PEM格式的字符串: ``` PrivateKey privateKey = ... // 获取RSA私钥 PublicKey publicKey = ... // 获取RSA公钥 Base64.Encoder encoder = Base64.getEncoder(); String privateKeyStr = encoder.encodeToString(privateKey.getEncoded()); String publicKeyStr = encoder.encodeToString(publicKey.getEncoded()); String pemPrivateKey = "-----BEGIN PRIVATE KEY-----\n" + privateKeyStr + "\n-----END PRIVATE KEY-----"; String pemPublicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKeyStr + "\n-----END PUBLIC KEY-----"; ``` 以上代码仅供参考,具体实现方式还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值