我想知道如何使用我的 C 源文件中的 OpenSSL 库生成 RSA 私钥。我知道如何使用终端命令生成它。
实际上,我的
server.c
文件会生成私钥并发送给client.c
。
如果可能,请帮助我提供一些源代码,也感谢任何其他帮助。
我使用的是 Linux 机器。
#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);
我想知道如何在我的 c 源文件中使用 openssl 库生成 RSA 私钥?
生成密钥很容易。只需使用
RSA_generate_key_ex
。下面的程序向您展示了如何做到这一点。
保存公钥和私钥是另一回事,因为您需要知道格式。下面的程序向您展示了如何以多种格式执行此操作。
以下是各种功能和格式。
相关,请参阅“BEGIN RSA PRIVATE KEY”和“BEGIN PRIVATE KEY”有什么区别。它讨论了 SubjectPublicKeyInfo、PrivateKeyInfo 以及公钥和私钥之间的区别。
PEM_write_bio_RSAPublicKey(PKCS PEM 格式)。注意
BEGIN RSA PUBLIC KEY
:
$ cat rsa-public-1.pem
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAmkD+kwlbsmvyUYOBuVkyWDyBNxGQNJrKUZu97Z+hBG3OA85fY6oV
CdyoT+KDmtAFgTgY76KRkHVYQH3HYFpec5vNptLJNd6OVJCXR6NFUR2b7aHLkres
qyPMrAHNU16wp85qdHALNd72EoUPkVItHqaBOldx7J9vyUF3bpAWq4XxRlbiK9iv
Yuci4/jr4ppRoso6LZm9Inzm53zYt2AqHUZQ+3WgK+czZyk8tLGEN7rtLZ7opvVA
aAiRGWiUWACtRwmpAq2L5PuWZkYdQ7WQigkx5c3x8iBkeEh022zqYkGOTh2Ue4Uf
rIYuSqjLb2d+jkfZQ1S4LQLQPtMxNV/FqwIDAQAB
-----END RSA PUBLIC KEY-----
PEM_write_bio_PUBKEY(传统PEM格式)。注意
BEGIN PUBLIC KEY
:
$ cat rsa-public-2.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmkD+kwlbsmvyUYOBuVky
WDyBNxGQNJrKUZu97Z+hBG3OA85fY6oVCdyoT+KDmtAFgTgY76KRkHVYQH3HYFpe
c5vNptLJNd6OVJCXR6NFUR2b7aHLkresqyPMrAHNU16wp85qdHALNd72EoUPkVIt
HqaBOldx7J9vyUF3bpAWq4XxRlbiK9ivYuci4/jr4ppRoso6LZm9Inzm53zYt2Aq
HUZQ+3WgK+czZyk8tLGEN7rtLZ7opvVAaAiRGWiUWACtRwmpAq2L5PuWZkYdQ7WQ
igkx5c3x8iBkeEh022zqYkGOTh2Ue4UfrIYuSqjLb2d+jkfZQ1S4LQLQPtMxNV/F
qwIDAQAB
-----END PUBLIC KEY-----
PEM_write_bio_PrivateKey(PEM)。注意
BEGIN PRIVATE KEY
:
$ cat rsa-private-1.pem
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaQP6TCVuya/JR
g4G5WTJYPIE3EZA0mspRm73tn6EEbc4Dzl9jqhUJ3KhP4oOa0AWBOBjvopGQdVhA
fcdgWl5zm82m0sk13o5UkJdHo0VRHZvtocuSt6yrI8ysAc1TXrCnzmp0cAs13vYS
hQ+RUi0epoE6V3Hsn2/JQXdukBarhfFGVuIr2K9i5yLj+OvimlGiyjotmb0ifObn
fNi3YCodRlD7daAr5zNnKTy0sYQ3uu0tnuim9UBoCJEZaJRYAK1HCakCrYvk+5Zm
Rh1DtZCKCTHlzfHyIGR4SHTbbOpiQY5OHZR7hR+shi5KqMtvZ36OR9lDVLgtAtA+
0zE1X8WrA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FLteIV702MtwPLYH92T4CbG1hmSxoCHzCtzeSNy2t6wNA6YvYfX4xt/ea
vIV5WeeTxEK2+3lzPYFD4O0CgYA+KGJsXM77I5bgowfFh1JnKpBLmBEy3B5h91E8
1jhjGSIptAI0qQ81hG4TgTrLNIA6d63jZZWlSpqjZndZsVmpHH+/IDjsXGIjuXs6
GF6WF6Tm9Txva7RAERY5oP+LIw1odum3qHfyw/D/IFjBPEhNworiJNk/JQgMt/G1
07j/AQKBgHF2AoovqkCVx0TyggVkwBxwrQvR4jmUYmLKYWYc+IkDQzESXIUme61b
OZ7OfbTKQHBTz7i8bXK5mWSbNgmhcVsKHnbPyXKIrL7p+j2b2gdSNAaP1c5XFI/l
BvQSuumzUXbzQHbsYjn/4phOv3AK1xGLhnVI69QpaCYYuuPyfm1X
-----END RSA PRIVATE KEY-----
i2d_RSAPublicKey_bio(ASN.1/DER):
$ dumpasn1 rsa-public.der
0 266: SEQUENCE {
4 257: INTEGER
: 00 9A 40 FE 93 09 5B B2 6B F2 51 83 81 B9 59 32
: 58 3C 81 37 11 90 34 9A CA 51 9B BD ED 9F A1 04
: 6D CE 03 CE 5F 63 AA 15 09 DC A8 4F E2 83 9A D0
: 05 81 38 18 EF A2 91 90 75 58 40 7D C7 60 5A 5E
: 73 9B CD A6 D2 C9 35 DE 8E 54 90 97 47 A3 45 51
: 1D 9B ED A1 CB 92 B7 AC AB 23 CC AC 01 CD 53 5E
: B0 A7 CE 6A 74 70 0B 35 DE F6 12 85 0F 91 52 2D
: 1E A6 81 3A 57 71 EC 9F 6F C9 41 77 6E 90 16 AB
: [ Another 129 bytes skipped ]
265 3: INTEGER 65537
: }
0 warnings, 0 errors.
i2d_RSAPrivateKey_bio(ASN.1/DER):
$ dumpasn1 rsa-private.der
0 1187: SEQUENCE {
4 1: INTEGER 0
7 257: INTEGER
: 00 9A 40 FE 93 09 5B B2 6B F2 51 83 81 B9 59 32
: 58 3C 81 37 11 90 34 9A CA 51 9B BD ED 9F A1 04
: 6D CE 03 CE 5F 63 AA 15 09 DC A8 4F E2 83 9A D0
: 05 81 38 18 EF A2 91 90 75 58 40 7D C7 60 5A 5E
: 73 9B CD A6 D2 C9 35 DE 8E 54 90 97 47 A3 45 51
: 1D 9B ED A1 CB 92 B7 AC AB 23 CC AC 01 CD 53 5E
: B0 A7 CE 6A 74 70 0B 35 DE F6 12 85 0F 91 52 2D
: 1E A6 81 3A 57 71 EC 9F 6F C9 41 77 6E 90 16 AB
: [ Another 129 bytes skipped ]
268 3: INTEGER 65537
273 256: INTEGER
: 5B 26 C3 08 92 44 1C 68 0B 76 3D B6 BF 97 44 D7
: BD 51 42 00 5F ED A5 F6 9E 48 E0 EF F5 1F 4A 25
: B4 D2 AC F5 D3 48 23 4C 83 96 F6 35 CD 3D 03 65
: ED 58 FF EB 43 6E AF 25 98 73 EB 9F 90 81 76 44
: EC B1 96 BE 02 66 91 D0 1B 8A A1 CA 94 61 96 B0
: 5F 7F 09 D7 E9 28 1E A1 B6 F2 F0 45 D4 56 4A A3
: 2B 56 1F B4 14 AD 9B 34 D8 C6 52 D8 C3 30 03 37
: 94 7E D2 C1 EF B1 3A F9 EC BC 81 DD D4 C9 80 AB
: [ Another 128 bytes skipped ]
...
该程序是用 C++ 编写的,即使您有 C 标签。它使我们能够避免大量的错误检查和清理,因为它是自动的。而且很容易转换回 C。
#include <memory>
using std::unique_ptr;
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#include <cassert>
#define ASSERT assert
using BN_ptr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
using EVP_KEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
using BIO_FILE_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;
int main(int argc, char* argv[])
{
int rc;
RSA_ptr rsa(RSA_new(), ::RSA_free);
BN_ptr bn(BN_new(), ::BN_free);
BIO_FILE_ptr pem1(BIO_new_file("rsa-public-1.pem", "w"), ::BIO_free);
BIO_FILE_ptr pem2(BIO_new_file("rsa-public-2.pem", "w"), ::BIO_free);
BIO_FILE_ptr pem3(BIO_new_file("rsa-private-1.pem", "w"), ::BIO_free);
BIO_FILE_ptr pem4(BIO_new_file("rsa-private-2.pem", "w"), ::BIO_free);
BIO_FILE_ptr pem5(BIO_new_file("rsa-private-3.pem", "w"), ::BIO_free);
BIO_FILE_ptr der1(BIO_new_file("rsa-public.der", "w"), ::BIO_free);
BIO_FILE_ptr der2(BIO_new_file("rsa-private.der", "w"), ::BIO_free);
rc = BN_set_word(bn.get(), RSA_F4);
ASSERT(rc == 1);
// Generate key
rc = RSA_generate_key_ex(rsa.get(), 2048, bn.get(), NULL);
ASSERT(rc == 1);
// Convert RSA to PKEY
EVP_KEY_ptr pkey(EVP_PKEY_new(), ::EVP_PKEY_free);
rc = EVP_PKEY_set1_RSA(pkey.get(), rsa.get());
ASSERT(rc == 1);
//////////
// Write public key in ASN.1/DER
rc = i2d_RSAPublicKey_bio(der1.get(), rsa.get());
ASSERT(rc == 1);
// Write public key in PKCS PEM
rc = PEM_write_bio_RSAPublicKey(pem1.get(), rsa.get());
ASSERT(rc == 1);
// Write public key in Traditional PEM
rc = PEM_write_bio_PUBKEY(pem2.get(), pkey.get());
ASSERT(rc == 1);
//////////
// Write private key in ASN.1/DER
rc = i2d_RSAPrivateKey_bio(der2.get(), rsa.get());
ASSERT(rc == 1);
// Write private key in PKCS PEM.
rc = PEM_write_bio_PrivateKey(pem3.get(), pkey.get(), NULL, NULL, 0, NULL, NULL);
ASSERT(rc == 1);
// Write private key in PKCS PEM
rc = PEM_write_bio_PKCS8PrivateKey(pem4.get(), pkey.get(), NULL, NULL, 0, NULL, NULL);
ASSERT(rc == 1);
// Write private key in Traditional PEM
rc = PEM_write_bio_RSAPrivateKey(pem5.get(), rsa.get(), NULL, NULL, 0, NULL, NULL);
ASSERT(rc == 1);
return 0;
}
RSA_generate_key_ex
正确播种 PRNG 后,您可以使用 RAND_add
。
编辑: 虽然最初编写此答案时需要调用
RAND_add
,但当前版本的 OpenSSL 不再需要手动播种 PRNG。