Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

c++ - How to load an RSA key from binary data to an RSA structure using the OpenSSL C Library?

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);

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...