日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Pem私钥pkcs1和pkcs8之间互转

發布時間:2023/12/31 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pem私钥pkcs1和pkcs8之间互转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

pkcs8私鑰轉pkcs1私鑰

方法1:使用OpenSSL工具轉化

openssl rsa -in pkcs8.pem -out pkcs1.pem

命令執行完后,當前文件目錄下將出現一個名為pkcs1.pem的文件,即為pkcs1格式。

方法2:通過代碼實現

jar包版本

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcpkix-jdk18on</artifactId><version>1.72</version></dependency>

代碼如下

private static final String CHARSET = "utf-8";/*** pkcs8私鑰文件轉化為pkcs1私鑰文件* @param pkcs8Key* @param pkcs1KeyFilePath* @param pkcs1KeyFileName* @return* @throws Exception*/public static File formatPkcs8ToPkcs1(File pkcs8Key,String pkcs1KeyFilePath,String pkcs1KeyFileName) throws Exception {PemObject object = null;try (PemReader pemReader = new PemReader(new FileReader(pkcs8Key))){object = pemReader.readPemObject();}/**將私鑰從PKCS8轉換為PKCS1**/PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(object.getContent());ASN1Encodable encodable = pkInfo.parsePrivateKey();ASN1Primitive primitive = encodable.toASN1Primitive();byte[] privateKeyPKCS1 = primitive.getEncoded();/**將PKCS1中的私鑰轉換為PEM**/PemObject pemObject = new PemObject("RSA PRIVATE KEY", privateKeyPKCS1);try(StringWriter stringWriter = new StringWriter()){PemWriter pemWriter = new PemWriter(stringWriter);pemWriter.writeObject(pemObject);pemWriter.flush();String pemString = stringWriter.toString();File pem = new File(pkcs1KeyFilePath, pkcs1KeyFileName);FileUtils.writeStringToFile(pem, pemString, CHARSET);return pem;}}

方法3:通過hutool實現

該實現方法通過hutool讀取秘鑰文件,既沒有降低代碼復雜度,也沒有減少jar包依賴,并不推薦使用。

jar版本

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.8</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcpkix-jdk18on</artifactId><version>1.72</version></dependency>

代碼如下

private static final String CHARSET = "utf-8";/*** pkcs8私鑰轉化為pkcs1私鑰* @param pkcs8Key* @param pkcs1KeyFilePath* @param pkcs1KeyFileName* @return* @throws Exception*/public static File formatPkcs8ToPkcs1ByHutool(File pkcs8Key,String pkcs1KeyFilePath,String pkcs1KeyFileName) throws Exception {PrivateKey privKeyInfo = PemUtil.readPemPrivateKey(new FileInputStream(pkcs8Key));/**將私鑰從PKCS8轉換為PKCS1**/PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(privKeyInfo.getEncoded());ASN1Encodable encodable = pkInfo.parsePrivateKey();ASN1Primitive primitive = encodable.toASN1Primitive();byte[] privateKeyPKCS1 = primitive.getEncoded();/**將PKCS1中的私鑰轉換為PEM**/PemObject pemObject = new PemObject("RSA PRIVATE KEY", privateKeyPKCS1);try(StringWriter stringWriter = new StringWriter()){PemWriter pemWriter = new PemWriter(stringWriter);pemWriter.writeObject(pemObject);pemWriter.flush();String pemString = stringWriter.toString();File pem = new File(pkcs1KeyFilePath, pkcs1KeyFileName);FileUtils.writeStringToFile(pem, pemString, CHARSET);return pem;}}

pkcs1私鑰轉pkcs8私鑰

方法1:使用OpenSSL工具轉化

openssl pkcs8 -topk8 -inform PEM -in rsa_private_key_pkcs1.pem -outform PEM -out rsa_private_key_pkcs8.pem -nocrypt

命令執行完后,當前文件目錄下將會出現一個名為rsa_private_key_pkcs8.pem的文件,即為pkcs8格式。

方法2:通過代碼實現

jar包版本

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcpkix-jdk18on</artifactId><version>1.72</version></dependency>

代碼如下

private static final String CHARSET = "utf-8";/***靜態代碼塊創建Bouncy Castle提供者*/static{org.bouncycastle.jce.provider.BouncyCastleProvider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();Security.addProvider(provider);}/*** pkcs1私鑰轉化為pkcs8私鑰* @param pkcs1Key* @param pkcs8KeyFilePath* @param pkcs8KeyFileName* @return* @throws Exception*/public static File formatPkcs1ToPkcs8(File pkcs1Key,String pkcs8KeyFilePath,String pkcs8KeyFileName) throws Exception {PemObject object = null;try (PemReader pemReader = new PemReader(new FileReader(pkcs1Key))){object = pemReader.readPemObject();}PrivateKey privKeyInfo = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(object.getContent()));/**將PKCS8中的私鑰轉換為PEM**/PemObject pemObject = new PemObject("PRIVATE KEY", privKeyInfo.getEncoded());try(StringWriter stringWriter = new StringWriter()){PemWriter pemWriter = new PemWriter(stringWriter);pemWriter.writeObject(pemObject);pemWriter.flush();String pemString = stringWriter.toString();File pem = new File(pkcs8KeyFilePath, pkcs8KeyFileName);FileUtils.writeStringToFile(pem, pemString, CHARSET);return pem;}}

方法3:通過第三方工具集hutool實現

hutool在5.8.5版本已經提供對pkcs#1格式秘鑰的支持。

jar版本

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.8</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcpkix-jdk18on</artifactId><version>1.72</version></dependency>

代碼如下

private static final String CHARSET = "utf-8";/*** pkcs1私鑰轉化為pkcs8私鑰* @param pkcs1Key* @param pkcs8KeyFilePath* @param pkcs8KeyFileName* @return* @throws Exception*/public static File formatPkcs1ToPkcs8ByHutool(File pkcs1Key,String pkcs8KeyFilePath,String pkcs8KeyFileName) throws Exception {PrivateKey privKeyInfo = PemUtil.readPemPrivateKey(new FileInputStream(pkcs1Key));/**將PKCS8中的私鑰轉換為PEM**/PemObject pemObject = new PemObject("PRIVATE KEY", privKeyInfo.getEncoded());try(StringWriter stringWriter = new StringWriter()){PemWriter pemWriter = new PemWriter(stringWriter);pemWriter.writeObject(pemObject);pemWriter.flush();String pemString = stringWriter.toString();File pem = new File(pkcs8KeyFilePath, pkcs8KeyFileName);FileUtils.writeStringToFile(pem, pemString, CHARSET);return pem;}}

總結

以上是生活随笔為你收集整理的Pem私钥pkcs1和pkcs8之间互转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。