基于OpenSSL 1.1.0g版本的RSA128/ECB/PKCS1padding实现。
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <string.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/rand.h>
RSA *generate_rsa_key_128(void)
{
RSA *rsa = NULL;
char random_seed[16];
BIGNUM *bn = NULL;
if (RAND_bytes(random_seed, 16) <= 0) {
goto err;
}
RAND_seed(random_seed, sizeof(random_seed));
bn = BN_new();
if (bn == NULL) {
goto err;
}
if (!BN_set_word(bn, RSA_F4)) {
goto err;
}
rsa = RSA_new();
if (rsa == NULL) {
goto err;
}
if (!RSA_generate_key_ex(rsa, 1024, bn, NULL)) {
rsa = NULL;
goto err;
}
err:
BN_free(bn);
bn = NULL;
return rsa;
}
int rsa_public_key_to_x509(const RSA *rsa, char **rsa_public_key_x509, unsigned int *rsa_public_key_x509_len)
{
int ret = 0;
if (rsa == NULL || rsa_public_key_x509 == NULL || rsa_public_key_x509_len == NULL) {
goto err;
}
ret = i2d_RSA_PUBKEY(rsa, rsa_public_key_x509);
*rsa_public_key_x509_len = ret;
err:
return ret;
}
RSA *rsa_public_key_from_x509(char *rsa_public_key_x509, unsigned int rsa_public_key_x509_len)
{
RSA *ret = NULL;
char *p = NULL;
if (rsa_public_key_x509 == NULL || rsa_public_key_x509_len == 0) {
goto err;
}
// use p as a temporary variable is mandatory.
p = rsa_public_key_x509;
ret = d2i_RSA_PUBKEY(NULL, &p, rsa_public_key_x509_len);
err:
return ret;
}
int rsa_public_encrypt_128_pkcs1padding(const char *plain_text, unsigned int plain_text_len, RSA *rsa,
char **cipher_text, unsigned int *cipher_text_len)
{
int ret = 1;
if (plain_text == NULL || plain_text_len > 128 - RSA_PKCS1_PADDING_SIZE ||
rsa == NULL || cipher_text == NULL || cipher_text_len == NULL) {
return -1;
}
if (RSA_size(rsa) != 128) {
return -1;
}
*cipher_text = (char *)malloc(128);
if (*cipher_text == NULL) {
goto err;
}
*cipher_text_len = RSA_public_encrypt(plain_text_len, plain_text, *cipher_text, rsa, RSA_PKCS1_PADDING);
if (*cipher_text_len <= 0) {
goto err;
}
ret = 0;
err:
return ret;
}
int rsa_private_decrypt_128_pkcs1padding(const char *cipher_text, unsigned int cipher_text_len, RSA *rsa,
char **plain_text, unsigned int *plain_text_len)
{
int ret = 1;
if (cipher_text == NULL || rsa == NULL || plain_text == NULL || plain_text_len == NULL) {
return -1;
}
if (RSA_size(rsa) != 128) {
return -1;
}
*plain_text = (char *)malloc(128);
if (*plain_text == NULL) {
goto err;
}
*plain_text_len = RSA_private_decrypt(cipher_text_len, cipher_text, *plain_text, rsa, RSA_PKCS1_PADDING);
if (*plain_text_len <= 0) {
goto err;
}
ret = 0;
err:
return ret;
}
int main(void)
{
int ret = 1;
RSA *rsa = NULL;
char *public_key_x509 = NULL;
unsigned int public_key_x509_len = 0;
RSA *rsa_cloud = NULL;
char *message = "longshine is a good man!";
unsigned int message_len = 24;
char *plain_text = NULL;
unsigned int plain_text_len = 0;
char *cipher_text = NULL;
unsigned int cipher_text_len = 0;
rsa = generate_rsa_key_128();
if (rsa == NULL) {
printf("generate rsa key failed\n");
goto err;
} else {
printf("generate rsa key success:\n");
RSA_print_fp(stdout, rsa, 8);
}
ret = rsa_public_key_to_x509(rsa, &public_key_x509, &public_key_x509_len);
if (ret <= 0) {
printf("convert the rsa public key to x509 failed\n");
goto err;
} else {
printf("convert the rsa public key to x509 success:\n");
BIO_dump_fp(stdout, public_key_x509, public_key_x509_len);
}
rsa_cloud = rsa_public_key_from_x509(public_key_x509, public_key_x509_len);
if (rsa_cloud == NULL) {
printf("convert the rsa public key from x509 failed\n");
goto err;
} else {
printf("convert the rsa public key from x509 success\n");
RSA_print_fp(stdout, rsa_cloud, 8);
}
ret = rsa_public_encrypt_128_pkcs1padding(message, message_len, rsa_cloud, &cipher_text, &cipher_text_len);
if (ret) {
printf("rsa public encrypt on cloud failed\n");
goto err;
} else {
printf("rsa public encrypt on cloud success\n");
BIO_dump_fp(stdout, cipher_text, cipher_text_len);
}
ret = rsa_private_decrypt_128_pkcs1padding(cipher_text, cipher_text_len, rsa, &plain_text, &plain_text_len);
if (ret) {
printf("rsa private decrypt failed\n");
goto err;
} else {
printf("rsa private decrypt success\n");
BIO_dump_fp(stdout, plain_text, plain_text_len);
}
err:
free(plain_text);
plain_text = NULL;
free(cipher_text);
cipher_text = NULL;
RSA_free(rsa_cloud);
rsa_cloud = NULL;
free(public_key_x509);
public_key_x509 = NULL;
RSA_free(rsa);
rsa = NULL;
return ret;
}