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>
代码如下
privatestaticfinalString CHARSET ="utf-8";/**
* pkcs8私钥文件转化为pkcs1私钥文件
* @param pkcs8Key
* @param pkcs1KeyFilePath
* @param pkcs1KeyFileName
* @return
* @throws Exception
*/publicstaticFileformatPkcs8ToPkcs1(File pkcs8Key,String pkcs1KeyFilePath,String pkcs1KeyFileName)throwsException{PemObject object =null;try(PemReader pemReader =newPemReader(newFileReader(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 =newPemObject("RSA PRIVATE KEY", privateKeyPKCS1);try(StringWriter stringWriter =newStringWriter()){PemWriter pemWriter =newPemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.flush();String pemString = stringWriter.toString();File pem =newFile(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>
代码如下
privatestaticfinalString CHARSET ="utf-8";/**
* pkcs8私钥转化为pkcs1私钥
* @param pkcs8Key
* @param pkcs1KeyFilePath
* @param pkcs1KeyFileName
* @return
* @throws Exception
*/publicstaticFileformatPkcs8ToPkcs1ByHutool(File pkcs8Key,String pkcs1KeyFilePath,String pkcs1KeyFileName)throwsException{PrivateKey privKeyInfo =PemUtil.readPemPrivateKey(newFileInputStream(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 =newPemObject("RSA PRIVATE KEY", privateKeyPKCS1);try(StringWriter stringWriter =newStringWriter()){PemWriter pemWriter =newPemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.flush();String pemString = stringWriter.toString();File pem =newFile(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>
代码如下
privatestaticfinalString CHARSET ="utf-8";/***
静态代码块创建Bouncy Castle提供者
*/static{org.bouncycastle.jce.provider.BouncyCastleProvider provider =neworg.bouncycastle.jce.provider.BouncyCastleProvider();Security.addProvider(provider);}/**
* pkcs1私钥转化为pkcs8私钥
* @param pkcs1Key
* @param pkcs8KeyFilePath
* @param pkcs8KeyFileName
* @return
* @throws Exception
*/publicstaticFileformatPkcs1ToPkcs8(File pkcs1Key,String pkcs8KeyFilePath,String pkcs8KeyFileName)throwsException{PemObject object =null;try(PemReader pemReader =newPemReader(newFileReader(pkcs1Key))){
object = pemReader.readPemObject();}PrivateKey privKeyInfo =KeyFactory.getInstance("RSA").generatePrivate(newPKCS8EncodedKeySpec(object.getContent()));/**将PKCS8中的私钥转换为PEM**/PemObject pemObject =newPemObject("PRIVATE KEY", privKeyInfo.getEncoded());try(StringWriter stringWriter =newStringWriter()){PemWriter pemWriter =newPemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.flush();String pemString = stringWriter.toString();File pem =newFile(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>
代码如下
privatestaticfinalString CHARSET ="utf-8";/**
* pkcs1私钥转化为pkcs8私钥
* @param pkcs1Key
* @param pkcs8KeyFilePath
* @param pkcs8KeyFileName
* @return
* @throws Exception
*/publicstaticFileformatPkcs1ToPkcs8ByHutool(File pkcs1Key,String pkcs8KeyFilePath,String pkcs8KeyFileName)throwsException{PrivateKey privKeyInfo =PemUtil.readPemPrivateKey(newFileInputStream(pkcs1Key));/**将PKCS8中的私钥转换为PEM**/PemObject pemObject =newPemObject("PRIVATE KEY", privKeyInfo.getEncoded());try(StringWriter stringWriter =newStringWriter()){PemWriter pemWriter =newPemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.flush();String pemString = stringWriter.toString();File pem =newFile(pkcs8KeyFilePath, pkcs8KeyFileName);FileUtils.writeStringToFile(pem, pemString, CHARSET);return pem;}}
版权归原作者 Alex_Jwell 所有, 如有侵权,请联系我们删除。