RSA加密

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>


int base64_encode(char *in_str, int in_len, char *out_str)
{
BIO *b64, *bio;
BUF_MEM *bptr = NULL;
size_t size = 0;

if (in_str == NULL || out_str == NULL)
return -1;

b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);

BIO_write(bio, in_str, in_len);
BIO_flush(bio);

BIO_get_mem_ptr(bio, &bptr);
memcpy(out_str, bptr->data, bptr->length);
out_str[bptr->length] = '\0';
size = bptr->length;

BIO_free_all(bio);
return size;
}

int base64_decode(char *in_str, int in_len, char *out_str)
{
BIO *b64, *bio;
BUF_MEM *bptr = NULL;
int counts;
int size = 0;

if (in_str == NULL || out_str == NULL)
return -1;

b64 = BIO_new(BIO_f_base64());
//BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

bio = BIO_new_mem_buf(in_str, in_len);
bio = BIO_push(b64, bio);

size = BIO_read(bio, out_str, in_len);
out_str[size] = '\0';

BIO_free_all(bio);
return size;
}


int main(int argc, char** argv)
{

char data[3048];
char out[2048];
char data_base64_pre[3048];
char data_base64[3048];
char data_unbase64[3048];

char *pub_key_path = argv[1];
char *priv_key_path = argv[2];
char *data_path = argv[3];
RSA *pub_rsa;
RSA *priv_rsa;
FILE *file;
int flen,rsa_len;
int enc_len = 0;
int dec_len = 0;
int data_len = 0;
int data_unbase64_len = 0;
int data_base64_len = 0;
int data_base64_pre_len = 0;

char* enc_buf;
char* dec_buf;

BIO* bio_data;

BIO* bio;
BIO* bio_enc;

BIO* bio_64;
BUF_MEM *bptr;

if (argc != 4) {
printf("\n usage:\n");
printf("\t %s pub_key_file priv_key_file data_file\n", argv[0]);
exit(0);
}

if((file=fopen(pub_key_path,"r"))==NULL){
perror("open public key file error");
return NULL;
}
if((pub_rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==NULL){
ERR_print_errors_fp(stdout);
return NULL;
}
fclose(file);

if((file=fopen(priv_key_path,"r"))==NULL){
perror("open private key file error");
return NULL;
}
if((priv_rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==NULL){
ERR_print_errors_fp(stdout);
return NULL;
}
fclose(file);

bzero(data, sizeof(data));
if((file=fopen(data_path,"r"))==NULL){
perror("open private key file error");
return NULL;
}
data_len = fread(data, 1, sizeof(data), file);
printf("read data length:%d\n", data_len);
fclose(file);

bio = BIO_new(BIO_s_mem());
BIO_write(bio, data, data_len);

bio_enc = BIO_new(BIO_s_mem());

rsa_len = RSA_size(pub_rsa);
//printf("pub rsa size:%d\n", rsa_len);
enc_buf = malloc(rsa_len);
while(1) {
char data[100];
int data_len = BIO_read(bio, data, sizeof(data));
// printf("read bytes:%d\n", data_len);
if (data_len <= 0) {
break;
}
bzero(enc_buf, rsa_len);
enc_len = RSA_public_encrypt(data_len, data, enc_buf, pub_rsa, RSA_PKCS1_PADDING);
//printf("encoded length:%d\n", enc_len);
BIO_write(bio_enc, enc_buf, enc_len);
}
BIO_free(bio);
BIO_flush(bio_enc);

// all the data has been encript
BIO_get_mem_ptr(bio_enc, &bptr);
memcpy(data_base64_pre, bptr->data, bptr->length);
data_base64_pre_len = bptr->length;

BIO_free(bio_enc);

//printf("length after encoded:%d\n", data_len);
//now base64 encode
data_base64_len = base64_encode(data_base64_pre, data_base64_pre_len, data_base64);
//printf("data length after base64 encode:%d\n", data_base64_len);
// printf("data content after base64 encode:\n%s\n", data_base64);
int i = 0;
for(i=0;i<1000;i++)
{
// now decript
data_unbase64_len = base64_decode(data_base64, data_base64_len, data_unbase64);
//printf("data length after base64 decode:%d\n", data_base64_len);
//printf("data content after base64 decode:%s\n", data_unbase64);

//printf("base64 decode length:%d\n", data_unbase64_len);
bio_64 = BIO_new(BIO_s_mem());
BIO_write(bio_64, data_unbase64, data_base64_len);

dec_buf = malloc(rsa_len);
while(1) {
dec_len = BIO_read(bio_64, dec_buf, rsa_len);
if(dec_len <= 0) {
break;
}
bzero(out, sizeof(out));
dec_len = RSA_private_decrypt(dec_len, dec_buf, out, priv_rsa, RSA_PKCS1_PADDING);
//printf("%s", out);
}
BIO_free(bio_64);
}
}