如何加载从二进制数据使用OpenSSL的C库RSA的结构RSA密钥? [英] How to load an RSA key from binary data to an RSA structure using the OpenSSL C Library?
问题描述
目前,我有我的私钥保存在一个文件,private.key,我用下面的函数来加载它:
Currently I have my private key saved in a file, private.key, and I use the following function to load it:
RSA * R = PEM_read_RSAPrivateKey(private.key,NULL,NULL,NULL);
RSA *r = PEM_read_RSAPrivateKey("private.key", NULL, NULL, NULL);
这完美的作品,但我不愉快的基于文件的格式;我要救我的纯二进制形式(即没有一个base64或类似)键在的char *
变量和加载/保存自/至它的关键。这样我有更多的自由:我可以存储密钥直接进入应用程序为const char []按键{0×01,0×02,...};
,发送通过网络插座等。
This works perfectly but I'm not happy with the file-based format; I want to save my key in pure binary form (ie, no base64 or similar) in a char*
variable and load/save the key from/to it. This way I have much more freedom: I'll be able to store the key directly into the application const char key[] { 0x01, 0x02, ... };
, send it over a network socket, etc.
但不幸的是我还没有找到一种方式来做到这一点。保存和加载我知道的一个关键的唯一方式读取/保存到直接在文件中。
Unfortunately though I haven't found a way to do that. The only way to save and load a key I know of reads/saves it to a file directly.
推荐答案
使用 d2i_RSAPrivateKey
直接包含二进制DER格式的缓冲加载:
Use d2i_RSAPrivateKey
to load directly from a buffer containing binary DER format:
const unsigned char *p = key;
RSA *r = d2i_RSAPrivateKey(NULL, &p, keylen);
这篇关于如何加载从二进制数据使用OpenSSL的C库RSA的结构RSA密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!