-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.cpp
186 lines (175 loc) · 5.28 KB
/
RSA.cpp
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
#include <stdlib.h>
#include <stdio.h>
#define LTM_DESC
#include <tomcrypt.h>
int main(void)
{
int err, hash_idx, prng_idx, res, padding, inpadding;
unsigned long l1, l2, i;
unsigned char pt[17], pt2[17], out[1024], out2[1024], out3[1024];
rsa_key key;
printf("**********************RSA加解密***********************\n");
while (true) {
printf("\n请输入文本串 s (16字节):\n");
scanf("%s", &pt);
printf("请选择加密机制:\t1.RSAES-PKCS1-V1_5\t2.RSAES_OAEP\n");
scanf("%d", &inpadding);
if (inpadding == 1)
padding = LTC_PKCS_1_V1_5;
else if(inpadding == 2)
padding = LTC_PKCS_1_OAEP;
else {
printf("输入了不支持的命令,请检查后重新输入!");
return 0;
}
/* register prng/hash */
if (register_prng(&sprng_desc) == -1) {
printf("Error registering sprng");
return EXIT_FAILURE;
}
/* register a math library (in this case TomsFastMath)*/
ltc_mp = ltm_desc;
if (register_hash(&sha1_desc) == -1) {
printf("Error registering sha1");
return EXIT_FAILURE;
}
hash_idx = find_hash("sha1");
prng_idx = find_prng("sprng");
/* make an RSA-1024 key */
if ((err = rsa_make_key(NULL, /* PRNG state */
prng_idx, /* PRNG idx */
1024 / 8, /* 1024-bit key */
65537, /* we like e=65537 */
&key) /* where to store the key */
) != CRYPT_OK) {
printf("rsa_make_key %s", error_to_string(err));
return EXIT_FAILURE;
}
/* fill in pt[] with a key we want to send ... */
l1 = sizeof(out);
if ((err = rsa_encrypt_key_ex(pt, /* data we wish to encrypt */
16, /* data is 16 bytes long */
out, /* where to store ciphertext */
&l1, /* length of ciphertext */
(unsigned char*)"TestApp", /* our lparam for this program */
7, /* lparam is 7 bytes long */
NULL, /* PRNG state */
prng_idx, /* prng idx */
hash_idx, /* hash idx */
padding,
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_encrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("\n加密后的密文 s' :\n");
for (int i = 0; i <= 127; i++)
printf("%02x", out[i]);
printf("\n");
/* now let’s decrypt the encrypted key */
l2 = sizeof(pt2);
if ((err = rsa_decrypt_key_ex(out, /* encrypted data */
l1, /* length of ciphertext */
pt2, /* where to put plaintext */
&l2, /* plaintext length */
(unsigned char*)"TestApp", /* lparam for this program */
7, /* lparam is 7 bytes long */
hash_idx, /* hash idx */
padding,
&res, /* validity of data */
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_decrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("将s'解密后的明文 s'':"); //%s\n",pt2
for (i = 0; i < 16; i++) {
printf("%c", pt2[i]);
}
printf("\n");
printf("原输入的明文 s :"); //%s\n",pt2
for (i = 0; i < 16; i++) {
printf("%c", pt[i]);
}
printf("\n");
if ((err = rsa_encrypt_key_ex(pt, /* data we wish to encrypt */
16, /* data is 16 bytes long */
out2, /* where to store ciphertext */
&l1, /* length of ciphertext */
(unsigned char*)"TestApp", /* our lparam for this program */
7, /* lparam is 7 bytes long */
NULL, /* PRNG state */
prng_idx, /* prng idx */
hash_idx, /* hash idx */
padding,
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_encrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("\n用相同密钥对同一个明文加密后的密文s'2\n:");
for (int i = 0; i <= 127; i++)
printf("%02x", out2[i]);
printf("\n");
l2 = sizeof(pt2);
if ((err = rsa_decrypt_key_ex(out2, /* encrypted data */
l1, /* length of ciphertext */
pt2, /* where to put plaintext */
&l2, /* plaintext length */
(unsigned char*)"TestApp", /* lparam for this program */
7, /* lparam is 7 bytes long */
hash_idx, /* hash idx */
padding,
&res, /* validity of data */
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_decrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("将s'2解密后的明文:");
for (i = 0; i < 16; i++) {
printf("%c", pt2[i]);
}
printf("\n");
if ((err = rsa_encrypt_key_ex(pt, /* data we wish to encrypt */
16, /* data is 16 bytes long */
out3, /* where to store ciphertext */
&l1, /* length of ciphertext */
(unsigned char*)"TestApp", /* our lparam for this program */
7, /* lparam is 7 bytes long */
NULL, /* PRNG state */
prng_idx, /* prng idx */
hash_idx, /* hash idx */
padding,
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_encrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("\n用相同密钥对同一个明文加密后的密文s'3:\n");
for (int i = 0; i <= 127; i++)
printf("%02x", out3[i]);
printf("\n");
l2 = sizeof(pt2);
if ((err = rsa_decrypt_key_ex(out3, /* encrypted data */
l1, /* length of ciphertext */
pt2, /* where to put plaintext */
&l2, /* plaintext length */
(unsigned char*)"TestApp", /* lparam for this program */
7, /* lparam is 7 bytes long */
hash_idx, /* hash idx */
padding,
&res, /* validity of data */
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_decrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("将s'3解密后的明文:");
for (i = 0; i < 16; i++) {
printf("%c", pt2[i]);
}
printf("\n");
}
return 0;
}