-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_keys.cpp
More file actions
executable file
·59 lines (46 loc) · 1.24 KB
/
generate_keys.cpp
File metadata and controls
executable file
·59 lines (46 loc) · 1.24 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
#include "generate_keys.h"
#include "utils.h"
void generate_keys(int key_length, char * pfile, char * rfile)
{
if ((key_length == 0) or (key_length >= 32768))
{
printf("Unsupported key lendth! Try again.\n");
return;
}
RSA *keypair = RSA_generate_key( key_length, 3, NULL, NULL );
BIO *publ = BIO_new( BIO_s_mem() );
BIO *pri = BIO_new( BIO_s_mem() );
PEM_write_bio_RSAPublicKey( publ, keypair );
PEM_write_bio_RSAPrivateKey( pri, keypair, NULL, NULL, 0, NULL, NULL );
char *key;
FILE *f;
FILE *f1;
key = key_to_str( pri );
printf("\n");
printf( "%s\n", key );
printf( "Enter file name for save: " );
printf("%s\n", rfile);
f = fopen( rfile, "wt" );
if (f == NULL)
{
printf("Enter file name!\n");
return;
}
fprintf( f, "%s", &key[0] );
free( key );
key = key_to_str( publ );
printf("\n");
printf( "%s\n", key );
printf( "Enter file name for save: " );
printf("%s\n", pfile);
f1 = fopen( pfile, "wt" );
if ((strcmp(pfile, rfile) == 0) or (f1 == NULL))
{
printf("Enter another file name!\n");
return;
}
fprintf( f1, "%s", key );
fclose(f);
fclose(f1);
free( key );
}