Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (61 loc) · 1.67 KB
/
Copy pathmain.cpp
File metadata and controls
71 lines (61 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
void generate_rsa(BIO* pub, BIO* priv)
{
// generate key
EVP_PKEY* pkey = EVP_RSA_gen(1024);
if (pkey == NULL)
{
fprintf(stderr, "error: rsa gen\n");
ERR_print_errors_fp(stderr);
return;
}
// write public key
if (PEM_write_bio_PUBKEY(pub, pkey) != 1)
{
fprintf(stderr, "error: write public key\n");
ERR_print_errors_fp(stderr);
EVP_PKEY_free(pkey);
return;
}
// write private key
if (PEM_write_bio_PrivateKey(priv, pkey, NULL, NULL, 0, NULL, NULL) != 1)
{
fprintf(stderr, "error: write private key\n");
ERR_print_errors_fp(stderr);
EVP_PKEY_free(pkey);
return;
}
// release key
EVP_PKEY_free(pkey);
}
int main()
{
// create BIO
BIO* pub_bio = BIO_new(BIO_s_mem());
BIO* priv_bio = BIO_new(BIO_s_mem());
if (pub_bio == NULL || priv_bio == NULL)
{
fprintf(stderr, "error: create BIO\n");
return 1;
}
// generate RSA keys
generate_rsa(pub_bio, priv_bio);
// print public key
BIO_flush(pub_bio);
BUF_MEM* pub_buf;
BIO_get_mem_ptr(pub_bio, &pub_buf);
BIO_set_close(pub_bio, BIO_NOCLOSE); // prevent BIO_free from freeing the buffer
printf("Public Key:\n%s\n", pub_buf->data);
// print private key
BIO_flush(priv_bio);
BUF_MEM* priv_buf;
BIO_get_mem_ptr(priv_bio, &priv_buf);
BIO_set_close(priv_bio, BIO_NOCLOSE); // prevent BIO_free from freeing the buffer
printf("Private Key:\n%s\n", priv_buf->data);
// free BIO
BIO_free_all(pub_bio);
BIO_free_all(priv_bio);
return 0;
}