如何使用 OpenSSL 生成 RSA 私钥? [英] How to generate RSA private key using OpenSSL?

查看:28
本文介绍了如何使用 OpenSSL 生成 RSA 私钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在我的 C 源文件中使用 OpenSSL 库生成 RSA 私钥.我知道如何使用终端命令生成它.

I want to know how to generate RSA private key using OpenSSL library in my C source file. I know how to generate it using terminal command.

实际上我的 server.c 文件会生成一个私钥并发送给 client.c如果可能,请帮助我提供一些源代码,否则将不胜感激.

Actually my server.c file will generate a private key and send to client.c Please help me with some source code if possible, otherwise any help will be appreciated.

我在 Linux 机器上工作.

I'm working on Linux machine.

推荐答案

#include <openssl/rsa.h>
#include <openssl/pem.h>

const int kBits = 1024;
const int kExp = 3;

int keylen;
char *pem_key;

RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0);

/* To get the C-string PEM form: */
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);

keylen = BIO_pending(bio);
pem_key = calloc(keylen+1, 1); /* Null-terminate */
BIO_read(bio, pem_key, keylen);

printf("%s", pem_key);

BIO_free_all(bio);
RSA_free(rsa);
free(pem_key);

这篇关于如何使用 OpenSSL 生成 RSA 私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆