公私钥保存成PEM文件
软件版本
openssl:版本1.1.1k,编译后的静态库
visual studio:版本2010
全部代码
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "compile_win_x86_lib\\lib\\libcrypto.lib")
#pragma comment(lib, "compile_win_x86_lib\\lib\\libssl.lib")
#include "openssl/rsa.h"
#include "openssl/pem.h"
#include "openssl/x509v3.h"
#include "openssl/pkcs12.h"
void testSavePem()
{
BIO* outBio = NULL;
char buf[4096];
RSA* rsaCtx = NULL;
PKCS8_PRIV_KEY_INFO* pkcs8Info = NULL;
EVP_PKEY* pEvKey = NULL;
X509_SIG* x509Sig = NULL;
memset(buf, 0x00, sizeof(buf));
rsaCtx = RSA_generate_key(2048, 65537, NULL, NULL);
/* public key PKCS#8 */
outBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSA_PUBKEY(outBio, rsaCtx);
BIO_read(outBio, buf, sizeof(buf));
printf("public key PKCS#8 pem file data=\n%s\n", buf);
BIO_free(outBio);
outBio = NULL;
/* public key PKCS#1 */
memset(buf, 0x00, sizeof(buf));
outBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPublicKey(outBio, rsaCtx);
BIO_read(outBio, buf, sizeof(buf));
printf("public key PKCS#1 pem file data=\n%s\n", buf);
BIO_free(outBio);
outBio = NULL;
/* private key PKCS#1 */
memset(buf, 0x00, sizeof(buf));
outBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(outBio, rsaCtx, NULL, NULL, 0, NULL, NULL);
BIO_read(outBio, buf, sizeof(buf));
printf("private key PKCS#1 pem file data=\n%s\n", buf);
BIO_free(outBio);
outBio = NULL;
/* private key PKCS#8 */
memset(buf, 0x00, sizeof(buf));
outBio = BIO_new(BIO_s_mem());
pEvKey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pEvKey, rsaCtx);
pkcs8Info = EVP_PKEY2PKCS8(pEvKey);
PEM_write_bio_PKCS8_PRIV_KEY_INFO(outBio, pkcs8Info);
BIO_read(outBio, buf, sizeof(buf));
printf("private key PKCS#8 pem file data=\n%s\n", buf);
BIO_free(outBio);
outBio = NULL;
EVP_PKEY_free(pEvKey);
pEvKey = NULL;
PKCS8_PRIV_KEY_INFO_free(pkcs8Info);
/* private key with password encrypt PKCS#1 */
memset(buf, 0x00, sizeof(buf));
outBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(outBio, rsaCtx, EVP_aes_256_cbc(), (unsigned char*)"123456", 6, NULL, NULL);//PKCS#1
BIO_read(outBio, buf, sizeof(buf));
printf("private key PKCS#1 pem file data with password=\n%s\n", buf);
BIO_free(outBio);
outBio = NULL;
/* private key with password encrypt PKCS#8 */
memset(buf, 0x00, sizeof(buf));
outBio = BIO_new(BIO_s_mem());
pEvKey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pEvKey, rsaCtx);
pkcs8Info = EVP_PKEY2PKCS8(pEvKey);
x509Sig = PKCS8_encrypt(-1, EVP_aes_256_cbc(), (char*)"123456", 6, NULL, 0, 0, pkcs8Info);
PEM_write_bio_PKCS8(outBio, x509Sig);
BIO_read(outBio, buf, sizeof(buf));
printf("private key PKCS#8 pem file data with password=\n%s\n", buf);
BIO_free(outBio);
outBio = NULL;
EVP_PKEY_free(pEvKey);
pEvKey = NULL;
X509_SIG_free(x509Sig);
PKCS8_PRIV_KEY_INFO_free(pkcs8Info);
}
代码说明
1、首先产生了RSA公私钥对;
2、然后对公钥私钥分别输出PEM格式数据;
3、格式有PKCS#1和PKCS#8,私钥分别有不带密码和带密码;
4、本测试直接printf输出PEM数据,没有保存文件,可根据各自需要新建文件保存。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhiececa
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
微信提示登录环境异常是什么意思原因
PHP中文网 04-09 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
微信人名旁边有个图标有什么用
PHP中文网 03-11