c - How to convert openssl RSA structure to char * and back?
问题描述
This question is likely to be a duplicate, I apologize if so, but cannot google a solution.
Given:
RSA* rsa = RSA_generate_key(2048, RSA_3, NULL, NULL);
I would like to have something like:
const char* pubKey = pubKeyFromRSA(rsa);
const char* privKey = privKeyFromRSA(rsa);
//and then convert it back
RSA* newRSA = RSAFromPrivKey(privKey);
How do I do that? Thanks
解决方案
Thanks to Michael Dorgan who pointed me in the right direction. I ended up having these two functions:
const char* keyFromRSA(RSA* rsa, bool isPrivate)
{
BIO *bio = BIO_new(BIO_s_mem());
if (isPrivate)
{
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);
}
else
{
PEM_write_bio_RSA_PUBKEY(bio, rsa);
}
const int keylen = BIO_pending(bio);
char* key = (char *)calloc(keylen+1, 1);
BIO_read(bio, key, keylen);
BIO_free_all(bio);
return key;
}
RSA* rsaFromPrivateKey(const char* aKey)
{
RSA* rsa = NULL;
BIO *bio = BIO_new_mem_buf(aKey, strlen(aKey));
PEM_read_bio_RSAPrivateKey(bio, &rsa, 0, 0);
BIO_free_all(bio);
return rsa;
}
推荐阅读
- .net-core - 在包参考中使用 * 占位符时,dotnet restore 未更新到新版本
- excel - 如何遍历 Excel 列以读取单元格值以用作我的 PDFmerge 代码工作的文件编号?
- dynamics-crm - 使用相关记录批量更新联系人
- javascript - 除了第一个,如何改变面板的电流?
- go - 确保 net/http 客户端通过 TLS 1.2 连接的传输选项
- c# - 在代理服务下我无法获取客户端 IP
- r - 有谁知道如何修复 Rmarkdown 中的错误 61
- apache-spark - 使用 hive 和 spark 选择 count(*) 问题
- sql - 从excel中的航班号字符串中提取航空公司
- reactjs - 这是一个thunk?