Generating RSA key pair
code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# include <openssl/bio.h>
# include <openssl/err.h>
# include <openssl/bn.h>
# include <openssl/rsa.h>
# include <openssl/evp.h>
# include <openssl/x509.h>
# include <openssl/pem.h>
# include <openssl/rand.h>
int main(int argc, char * argv[])
{
BIO *out = BIO_new(BIO_s_file());
BIO_write_filename(out, (char *)"bobkey.pem" );
BIGNUM *bn = BN_new();
BN_set_word(bn, RSA_F4);
RSA *rsa = RSA_new();
RSA_generate_key_ex(rsa, 2048 , bn, NULL);
PEM_write_bio_RSAPrivateKey(out
, rsa
, EVP_des_ede3_cbc()
, NULL, 0
, NULL, (void *)"passphrase" );
if (rsa) RSA_free(rsa);
if (bn) BN_free(bn);
if (out) BIO_free(out);
return 0 ;
}
Build test code for MingW
#!/bin/bash
g++ -I/d/workspace/github/altcoin/bitcoin-3 rd/ssl/include \
-o $1 -g -O0 $1 .cpp \
-L/d/workspace/github/altcoin/bitcoin-3 rd/ssl/lib -lcrypto -lssl -lgdi32
Output
cat bobkey.pem
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4 ,ENCRYPTED
DEK-Info: DES-EDE3-CBC,7FEF95ED86DB694B
CkXz9WNTShfC7DzLYPLpu9RxAbWyPYn5fvHaB67nK58UfzK5u8giabKfk/XK6I2M
...
xYx+DDsXtF5rNz94LoGl/8KF7C4gT6Hq6Yd0mEHjJhHzWcDN3gJG0GOByQdNNMWG
-----END RSA PRIVATE KEY-----
Check key pair with openssl rsa utility
openssl rsa -in bobkey.pem -text -noout
Prompt for pass phrase, the output as following:
Private-Key : (2048 bit )
modulus :
00:d2 :a6 :02 :a5 :2e :a1 :f9 :63 :89 :b1 :19 :01 :97 :7b :
b4 :15 :95 :46 :d0 :5f :67 :1a :db :a1 :14 :61 :06 :7d :1a :
...
a0 :8d :36 :95 :9c :ae :71 :fe :b1 :2a :e9 :b8 :e4 :af :85 :
43:0f
publicExponent : 65537 (0x10001 )
privateExponent :
38:31 :97 :25 :0f :a2 :a0 :0f :e0 :ac :80 :da :3e :d9 :fe :
...
04:60 :b3 :e0 :77 :00 :32 :a1 :e7 :bc :f8 :ef :2a :3e :07 :
d1
prime1 :
00:eb :ac :d0 :46 :72 :37 :66 :e5 :bc :c3 :93 :44 :48 :62 :
...
80:e6 :8d :ba :43 :80 :0c :06 :f7
prime2 :
00:e4 :d0 :aa :41 :ac :27 :2b :29 :c2 :da :6c :dd :a3 :7b :
...
94:0d :60 :a0 :66 :b6 :d4 :26 :a9
exponent1 :
36:a3 :78 :1d :fc :f8 :ef :38 :30 :0d :3f :a5 :43 :0a :ff :
...
52:db :64 :e5 :a5 :58 :24 :7d
exponent2 :
00:e1 :ef :48 :ae :60 :16 :ad :00 :27 :6f :16 :60 :23 :b5 :
...
20:3d :b5 :53 :ad :18 :8a :93 :41
coefficient :
00:8b :8e :98 :7f :05 :05 :13 :b5 :00 :cc :d3 :33 :bd :aa :
...
82:1c :75 :97 :2f :b2 :39 :16 :70