openssl pem.h 中提供了关于pem格式密钥对的操作接口

通常使用.pem的格式文件来保存openssl 生成的密钥对;

在终端下 cat xxx.pem 可以看到

-----BEGIN RSA PRIVATE KEY-----

XXXX


-----END RSA PRIVATE KEY-----

密钥数据进行了BASE64编码

1. 示例:将生成的密钥对保存成pem文件

void testWriteRSA2PEM()
{
//生成密钥对
RSA *r = RSA_new();
int bits = ;
BIGNUM *e = BN_new();
BN_set_word(e, );
RSA_generate_key_ex(r, bits, e, NULL); RSA_print_fp(stdout, r, ); BIO *out;
out = BIO_new_file("/Users/cocoajin/Desktop/opriv.pem","w");
//这里生成的私钥没有加密,可选加密
int ret = PEM_write_bio_RSAPrivateKey(out, r, NULL, NULL, , NULL, NULL);
printf("writepri:%d\n",ret);
BIO_flush(out);
BIO_free(out); out = BIO_new_file("/Users/cocoajin/Desktop/opub.pem","w");
ret = PEM_write_bio_RSAPublicKey(out, r);
printf("writepub:%d\n",ret);
BIO_flush(out);
BIO_free(out); BN_free(e);
RSA_free(r); }

在目标路径保存了生成的公钥opub.pem和私钥oprov.pem

输出日志

Private-Key: ( bit)
modulus:
:e6::f5:6f:4d:5e:c2:ad:0d:f5:::::
ca:6f::4f:3a:::e2:ee:a2:cc::::e2:
3d:0d:e3:0d:c0:6a:::b6:c7:de:e2:d6:::
9e::::dd:2d::c5:a8:f2:c1::8b::f9:
:0a:7a::
publicExponent: (0x10001)
privateExponent:
:ca:1b::6f:c5:de:c2:bf:2f:b6::f7:ad:d8:
::fc::fd:0a:e0::6b::::ea::8c:
b0:7a:d3:d8::b7:fb::f1:4e::9d:bf::3a:
e0:f3:fa::d3::fd:b0:f5:cd:5c::d1::8c:
:e3:fb:c7:
prime1:
:f6:d7::c4:1a:2f:eb:0b:c4:::3b:fe:f6:
0d:c1:::ca:6c::fc:de:d7::ac::9d::
:fe:9b
prime2:
:ef:::::4d:ac::aa:ed:e9:a7:1d:5d:
b1:7d:1c:1a:ef::8f:2d:bc:8a:e7::f5:f8::
::
exponent1:
:f4:d2:c0:6f:b8:ed::fa:::2f::::
b3::9f:c0:6c:b8:::b1:df:b4::fd:2a::
:ec:2b
exponent2:
:b5::c1::bc:3d::f8:::db:f9:5c:c4:
:d8::fe:::7e:a6:b4:6a:::db::d0:
::
coefficient:
0a:::2f:e9:ca:b6::::3b:5a::ea:5e:
8d:::b9:7a:::c4::cb::0e::6b:1b:
e5:c4
writepri:
writepub:
Program ended with exit code:

2. 示例:从pem文件中获取公钥私钥方式一(利用了BIO)

void testReadRSAFromPEM()
{
RSA *pubkey = RSA_new();
RSA *prikey = RSA_new(); BIO *pubio;
BIO *priio; priio = BIO_new_file("/Users/cocoajin/Desktop/opriv.pem", "rb");
prikey = PEM_read_bio_RSAPrivateKey(priio, &prikey, NULL, NULL); pubio = BIO_new_file("/Users/cocoajin/Desktop/opub.pem", "rb");
pubkey = PEM_read_bio_RSAPublicKey(pubio, &pubkey, NULL, NULL); RSA_print_fp(stdout, pubkey, );
RSA_print_fp(stdout, prikey, ); RSA_free(pubkey);
BIO_free(pubio);
RSA_free(prikey);
BIO_free(priio); }

从目标路径读取公钥opub.pem和私钥opriv.pem

输出日志

Public-Key: ( bit)
Modulus:
:e6::f5:6f:4d:5e:c2:ad:0d:f5:::::
ca:6f::4f:3a:::e2:ee:a2:cc::::e2:
3d:0d:e3:0d:c0:6a:::b6:c7:de:e2:d6:::
9e::::dd:2d::c5:a8:f2:c1::8b::f9:
:0a:7a::
Exponent: (0x10001)
Private-Key: ( bit)
modulus:
:e6::f5:6f:4d:5e:c2:ad:0d:f5:::::
ca:6f::4f:3a:::e2:ee:a2:cc::::e2:
3d:0d:e3:0d:c0:6a:::b6:c7:de:e2:d6:::
9e::::dd:2d::c5:a8:f2:c1::8b::f9:
:0a:7a::
publicExponent: (0x10001)
privateExponent:
:ca:1b::6f:c5:de:c2:bf:2f:b6::f7:ad:d8:
::fc::fd:0a:e0::6b::::ea::8c:
b0:7a:d3:d8::b7:fb::f1:4e::9d:bf::3a:
e0:f3:fa::d3::fd:b0:f5:cd:5c::d1::8c:
:e3:fb:c7:
prime1:
:f6:d7::c4:1a:2f:eb:0b:c4:::3b:fe:f6:
0d:c1:::ca:6c::fc:de:d7::ac::9d::
:fe:9b
prime2:
:ef:::::4d:ac::aa:ed:e9:a7:1d:5d:
b1:7d:1c:1a:ef::8f:2d:bc:8a:e7::f5:f8::
::
exponent1:
:f4:d2:c0:6f:b8:ed::fa:::2f::::
b3::9f:c0:6c:b8:::b1:df:b4::fd:2a::
:ec:2b
exponent2:
:b5::c1::bc:3d::f8:::db:f9:5c:c4:
:d8::fe:::7e:a6:b4:6a:::db::d0:
::
coefficient:
0a:::2f:e9:ca:b6::::3b:5a::ea:5e:
8d:::b9:7a:::c4::cb::0e::6b:1b:
e5:c4
Program ended with exit code:

3. 示例:从pem文件中读取公钥私钥方式二(利用FILE)

void testPEMReadRSA()
{
RSA *pubkey = RSA_new();
RSA *prikey = RSA_new(); FILE *pubf = fopen("/Users/cocoajin/Desktop/opub.pem", "rb");
pubkey = PEM_read_RSAPublicKey(pubf, &pubkey, NULL, NULL); FILE *prif = fopen("/Users/cocoajin/Desktop/opriv.pem", "rb");
prikey = PEM_read_RSAPrivateKey(prif, &prikey, NULL, NULL); RSA_print_fp(stdout, pubkey, );
RSA_print_fp(stdout, prikey, ); fclose(pubf);
fclose(prif);
RSA_free(pubkey);
RSA_free(prikey); }

输出日志

Public-Key: ( bit)
Modulus:
:e6::f5:6f:4d:5e:c2:ad:0d:f5:::::
ca:6f::4f:3a:::e2:ee:a2:cc::::e2:
3d:0d:e3:0d:c0:6a:::b6:c7:de:e2:d6:::
9e::::dd:2d::c5:a8:f2:c1::8b::f9:
:0a:7a::
Exponent: (0x10001)
Private-Key: ( bit)
modulus:
:e6::f5:6f:4d:5e:c2:ad:0d:f5:::::
ca:6f::4f:3a:::e2:ee:a2:cc::::e2:
3d:0d:e3:0d:c0:6a:::b6:c7:de:e2:d6:::
9e::::dd:2d::c5:a8:f2:c1::8b::f9:
:0a:7a::
publicExponent: (0x10001)
privateExponent:
:ca:1b::6f:c5:de:c2:bf:2f:b6::f7:ad:d8:
::fc::fd:0a:e0::6b::::ea::8c:
b0:7a:d3:d8::b7:fb::f1:4e::9d:bf::3a:
e0:f3:fa::d3::fd:b0:f5:cd:5c::d1::8c:
:e3:fb:c7:
prime1:
:f6:d7::c4:1a:2f:eb:0b:c4:::3b:fe:f6:
0d:c1:::ca:6c::fc:de:d7::ac::9d::
:fe:9b
prime2:
:ef:::::4d:ac::aa:ed:e9:a7:1d:5d:
b1:7d:1c:1a:ef::8f:2d:bc:8a:e7::f5:f8::
::
exponent1:
:f4:d2:c0:6f:b8:ed::fa:::2f::::
b3::9f:c0:6c:b8:::b1:df:b4::fd:2a::
:ec:2b
exponent2:
:b5::c1::bc:3d::f8:::db:f9:5c:c4:
:d8::fe:::7e:a6:b4:6a:::db::d0:
::
coefficient:
0a:::2f:e9:ca:b6::::3b:5a::ea:5e:
8d:::b9:7a:::c4::cb::0e::6b:1b:
e5:c4
Program ended with exit code:

4. 在终端下使用openssl命令生成公钥私钥

//生成1024位的RSA私钥
openssl genrsa -out private.pem //再由私钥生成公钥
openssl rsa -in private.pem -pubout -out public.pem //私钥文件private.pem
//公钥文件public.pem
//上面私钥是没加密的,可选加密,指定一个加密算法生成时输入密码

以上测试使用openssl 1.1.0c

参考:https://www.openssl.org/docs/man1.1.0/crypto/PEM_read_bio_RSAPublicKey.html

openssl pem 生成公钥和私钥及文件的更多相关文章

  1. openssl windows 生成公钥与私钥

    链接: https://pan.baidu.com/s/1qn-qeFxovor-vcAWFl8jIw 提取码: zy5v 一,下载安装windows平台openssl密钥生成工具,执行安装目录bin ...

  2. git 生成公钥、私钥方法与clone使用方法

    我的配置流程 Git配置 Git安装完之后,需做git配置.打开git bash,分别执行以下两句命令 git config --global user.name "用户名" gi ...

  3. Git简单生成生成公钥和私钥方法

    Git简单生成生成公钥和私钥方法 Git配置 Git安装完之后,需做最后一步配置.打开git bash,分别执行以下两句命令 git config --global user.name “用户名” g ...

  4. Git安装与使用(windows环境)(一)----Git安装、生成公钥和私钥、添加SSH

    安装 1.从官网下载git:http://git-scm.com/downloads 2.安装git,选择git组件安装,如下图 3.一直next,直到出现下面的窗口.这里是选择命令行形式.(可以理解 ...

  5. GIT生成公钥和私钥

    转载至:https://blog.csdn.net/gwz1196281550/article/details/80268200 打开 git bash! git config --global us ...

  6. MAC下用OPENSSL生成公钥和私钥

    MAC OS自带了OpenSSL,所以不用去编译那一坨跟SHIT一样的源码.直接在命令行里使用OPENSSL就可以. 打开命令行工具,然后输入 openssl打开openssl,接着只要三句命令就可以 ...

  7. 使用keytool生成公钥、私钥、证书并且读取出来,使用私钥签名jar并验证(转)

    参考链接:http://happyqing.iteye.com/blog/2139504 :https://blog.csdn.net/arjelarxfc/article/details/52461 ...

  8. xshell生成公钥和私钥

    一.打开你的xshell工具,工具栏有一个工具选项,点开选择新建用户密钥生成向导(如下图所示) 二. 点开之后就会如上图所示一样,点击选择下一步,出现如下,再点击下一步 点击完下一步会出现如下图所示 ...

  9. sourceTree 生成公钥和私钥 然后拉项目代码

    第一步 用sourceTree生成公钥pub和私钥文件ppk    打开sourceTree -> 工具 -> 创建或导入ssh密钥 -> 点击generate -> 上面会显 ...

随机推荐

  1. linux搭建微型git服务器

    1.安装git和git-core yum install git git-core -y 2.创建仓库 mkdir /home/git cd /home/git git init 3.设置可以远程pu ...

  2. HTA全解析:给VBS和JS一个强大的交互界面

    1.概述 HTA全称Html Application,在Windows中由mshta.exe加载执行,是一种本地程序.可使用html元素来构建界面,用JS或VBS来构建执行逻辑,从而强化Windows ...

  3. Android数字签名

    描述你对Android数字签名的理解? 数字签名就是为程序打上一个标记来作为自己的标识,是Android系统要求的,若一个Android程序没有经过数字签名,没办法安装到系统: 签名也是对apk的一种 ...

  4. nodejs的初学

    1.启服务器.先server.js,再命令行输入命令node server.js,打开浏览器输入http://127.0.0.1:2016可以看到有内容输出. server.js代码如下: var h ...

  5. gitlab多人协作开发

    gitlab多人协同工作 本文为亨利向<Git权威指南>的作者蒋鑫老师的答疑邮件写成. 这里特别感谢蒋鑫老师对我询问gitlab的协同工作流程问题的详细解答. 蒋鑫老师的细致专业的解答让我 ...

  6. Thinkphp更改子集

    //规格编辑 public function standard(){ //$id = $_GET['id'];//dump($id);die; $id = 388; //根据$id 到re_goods ...

  7. 题目描述: k一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

    时间限制:1秒     空间限制:32768k 斐波那契数列指的是这样一个数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,9 ...

  8. MVC中view和controller相互传值的方法

    MVC项目中,在view层如果使用前台框架,框架中会有封装好的相互传值方法.但是,那些postdata[][]方法不一定能够满足功能需求,反而一些常用的传值方法可能会刚好解决它们的不足.总结如下: 一 ...

  9. Thinkphp回顾(五)之前台模板中的基本语法

    一.导入CSS和JS文件 的三种方式  (了解) 1.link方式(常规) <link rel=’stylesheet’ type=’text/css’ href=’__PUBLIC__/Js/ ...

  10. JDBC 内部资料 请勿转载 谢谢合作

    一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connect ...