使用libopenssl从私钥转储公钥
Dumping public key from private using libopenssl
假设我想在我的c++应用程序中有一个类似openssl命令的代码。
openssl rsa -in private.pem -pubout -outform der -out ./out.pub
我该怎么做呢?
我在github上寻找一个样本,并提出了以下方案。
key = PEM_read_bio_RSAPrivateKey(bio, NULL, 0, NULL);
len = i2d_RSAPublicKey(key, &bufp);
它返回的值与我从命令行工具获得的值不同。我想没有从私钥到公钥的转换,它只是保存了私钥。谁能告诉我使用openssl lib从私有中获取pub密钥的正确方法?我也非常感谢任何关于openssl的pubpriv密钥示例的链接。
最后我在openssl内部找到了合适的源代码。下面是
期间发生的事情openssl rsa -in private。/out.pub
我从原始代码中删除了许多检查,使其更小。
#include <openssl/pem.h>
#include <openssl/x509.h>
EVP_PKEY *load_key(const char *file)
{
BIO *key=NULL;
EVP_PKEY *pkey=NULL;
key=BIO_new(BIO_s_file());
BIO_read_filename(key,file);
pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,NULL);
return pkey;
}
int _tmain(int argc, _TCHAR* argv[])
{
BIO *out=NULL;
out=BIO_new(BIO_s_file());
EVP_PKEY *pkey;
RSA *rsa=NULL;
char *infile = path_to_pem;
char *outfile = path_to_der;
pkey = load_key(infile);
if (pkey != NULL)
rsa = EVP_PKEY_get1_RSA(pkey);
EVP_PKEY_free(pkey);
BIO_write_filename(out,outfile);
i2d_RSA_PUBKEY_bio(out,rsa);
}
相关文章:
- 分段故障(堆芯转储)矢量
- Cppcheck生成xml转储文件
- 如何找出GDB的SIGTRAP核心转储的根本原因
- C++映射分割错误(核心转储)
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 将TPM公钥序列化为DER或PEM
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 浮点异常(核心转储)#694457
- 分段错误(核心转储)但无法弄清楚
- 链接到libkcapi时没有核心转储
- 检测到堆栈粉碎:已终止 中止(核心已转储)
- 正在处理故障(堆芯转储)
- 分段错误(核心转储) - 使用 SavedModel 的 Tensorflow C++ API 进行推断
- 我不知道为什么这段代码会让核心被转储?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 使用公钥加密消息:BER 解码错误
- 比较两个公钥与OpenSSL API
- 在基数排序中,我得到 munmap_chunk():无效指针和中止(核心转储).为什么?
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 使用libopenssl从私钥转储公钥