通过openssl源码生成国密SM2密钥对

以centos上通过openssl 1.1.1d实现为例:

1.下载openssl源码:

curl -o openssl-1.1.1d.tar.gz https://www.openssl.org/source/openssl-1.1.1d.tar.gz

2.解压源码包:

tar -xvf openssl-1.1.1d.tar.gz

3.进入解压出的openssl目录:

cd openssl-1.1.1d

4.配置生成makefile:

./config --prefix=/usr/local/openssl

5.编译、安装:

make install

6.设置openssl lib .so加载配置:

echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v

7.生成SM2密钥对:

1)进入生成密钥文件的目录,执行以下命令生成私钥

/usr/local/openssl/bin/openssl ecparam -genkey -name SM2 -out SM2PrivateKey.pem

2)执行以下命令生成公钥

/usr/local/openssl/bin/openssl ec -in SM2PrivateKey.pem -pubout -out SM2PublicKey.pem

结果示例:

国密SM2私钥SM2PrivateKey.pem

-----BEGIN EC PARAMETERS-----
BggqgRzPVQGCLQ==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIOHSA5sO6QbqmGM1mrtplIDUth92o3yyPh7R6C7jhsS2oAoGCCqBHM9V
AYItoUQDQgAE6fyV2irf4j5DxR3BRidJ3v30TFavyz6skDPjA8TQgoMJF3iCFbqV
B4GxVcjkqucDPZ1khgq9l72krT7Lg0Yqag==
-----END EC PRIVATE KEY-----

国密SM2公钥SM2PublicKey.pem

-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE6fyV2irf4j5DxR3BRidJ3v30TFav
yz6skDPjA8TQgoMJF3iCFbqVB4GxVcjkqucDPZ1khgq9l72krT7Lg0Yqag==
-----END PUBLIC KEY-----

你可能感兴趣的:(openssl)