sm2私钥、公钥转成pem
时间: 2023-07-10 10:43:28 浏览: 383
openssl SM2 密钥解析出公钥和私钥
将SM2私钥、公钥转换为PEM格式,可以按照以下步骤进行:
1. 私钥转换:
```java
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.crypto.util.PrivateKeyInfoParser;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import java.io.IOException;
import java.io.StringWriter;
import java.security.PrivateKey;
import java.security.interfaces.ECPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
public class SM2Util {
/**
* 将SM2私钥转换为PEM格式
*
* @param privateKey SM2私钥
* @return PEM格式的SM2私钥
* @throws IOException
*/
public static String privateKeyToPEM(ECPrivateKey privateKey) throws IOException {
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKey);
StringWriter stringWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(new PemObject("PRIVATE KEY", privateKeyInfo.getEncoded()));
pemWriter.close();
return stringWriter.toString();
}
/**
* 将PEM格式的SM2私钥转换为ECPrivateKey对象
*
* @param pem PEM格式的SM2私钥
* @return ECPrivateKey对象
* @throws Exception
*/
public static ECPrivateKey pemToPrivateKey(String pem) throws Exception {
PemReader pemReader = new PemReader(new StringReader(pem));
PemObject pemObject = pemReader.readPemObject();
byte[] derBytes = pemObject.getContent();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(derBytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
return (ECPrivateKey) keyFactory.generatePrivate(keySpec);
}
}
```
2. 公钥转换:
```java
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoParser;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import java.io.IOException;
import java.io.StringWriter;
import java.security.interfaces.ECPublicKey;
public class SM2Util {
/**
* 将SM2公钥转换为PEM格式
*
* @param publicKey SM2公钥
* @return PEM格式的SM2公钥
* @throws IOException
*/
public static String publicKeyToPEM(ECPublicKey publicKey) throws IOException {
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(new ECPublicKeyParameters(publicKey.getW(), ECUtil.getDomainParameters(publicKey)));
StringWriter stringWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKeyInfo.getEncoded()));
pemWriter.close();
return stringWriter.toString();
}
/**
* 将PEM格式的SM2公钥转换为ECPublicKey对象
*
* @param pem PEM格式的SM2公钥
* @return ECPublicKey对象
* @throws IOException
*/
public static ECPublicKey pemToPublicKey(String pem) throws IOException {
PemReader pemReader = new PemReader(new StringReader(pem));
PemObject pemObject = pemReader.readPemObject();
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pemObject.getContent());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyInfo.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
return (ECPublicKey) keyFactory.generatePublic(keySpec);
}
}
```
转换后,可以使用文本编辑器打开PEM格式的私钥、公钥文件,查看其中的密钥信息。
阅读全文