-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptogram.cpp
321 lines (291 loc) · 11.5 KB
/
cryptogram.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "cryptogram.h"
/*
RSA* createRSA(unsigned char *key ,int public_b)
{
RSA *rsa= NULL;
BIO *keybio ;
keybio = BIO_new_mem_buf(key, -1);
if (keybio==NULL)
{
printf( "Failed to create key BIO");
return 0;
}
if(public_b)
{
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
}
return rsa;
}
*/
bool create_RSA_key(char *pubAddr,char *privAddr)
{
int ret = 0;
RSA *r = NULL;
BIGNUM *bne = NULL;
BIO *bp_public = NULL, *bp_private = NULL;
int bits = 2048;
unsigned long e = RSA_F4;
bool retVal=true;
// 1. generate rsa key
bne = BN_new();
ret = BN_set_word(bne,e);
if(ret != 1){
cerr<<"BN" << endl;
return false;
}
r = RSA_new();
ret = RSA_generate_key_ex(r, bits, bne, NULL);
if(ret != 1){
cerr<<"RSA generate" << endl;
return false;
}
// 2. save public key
bp_public = BIO_new_file(pubAddr, "w+");
cerr<< "address is: " << pubAddr << endl;
ret = PEM_write_bio_RSAPublicKey(bp_public, r);
if(ret != 1){
cerr<< "write public key" << endl;
return false;
}
// 3. save private key
bp_private = BIO_new_file(privAddr, "w+");
ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL);
cerr << "write private key" << endl;
retVal=(ret == 1);
// 4.Free
BIO_free_all(bp_public);
BIO_free_all(bp_private);
RSA_free(r);
BN_free(bne);
return retVal;
}
RSA *getPrivateKey(char *privAddr){
BIO *bp_private = NULL;
bp_private = BIO_new_file(privAddr, "r");
RSA *rsa_privatekey = PEM_read_bio_RSAPrivateKey(bp_private, NULL, 0, NULL);
BIO_free(bp_private);
return rsa_privatekey;
}
void show_encrypted_massage(char* key,vector<string> com){
string command="";
for(int i=0;i<com.size(); i++)
command+=com[i]+" ";
cout << "encrypted massage is: "<< command << endl;
}
RSA *getPublicKey(char *pubAddr){
BIO *bp_public = NULL;
bp_public = BIO_new_file(pubAddr, "r");
RSA *rsa_publickey = PEM_read_bio_RSAPublicKey(bp_public, NULL, 0, NULL);
BIO_free(bp_public);
return rsa_publickey;
}
int private_encrypt(unsigned char * data,int data_len ,char * privateAddr,unsigned char *encrypted)
{
RSA * rsa = getPrivateKey(privateAddr);
int result = RSA_private_encrypt(data_len,data,encrypted,rsa,RSA_PKCS1_PADDING);
return result;
}
bool certificate_is_valid(char*buff_read){
vector<string> rcommand = mytokenizer(buff_read," ");
string t = "./DB/certificate/" +rcommand[1];
return file_exist(t.c_str());
}
int public_decrypt(unsigned char * enc_data,int data_len,char *publicAddr,unsigned char *decrypted)
{
RSA * rsa = getPublicKey(publicAddr);
int result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,RSA_PKCS1_PADDING);
return result;
}
int convert_size_t_to_int( size_t what )
{
if( what > INT_MAX ) {
return -1;
}
return static_cast<int>( what );
}
bool create_certificate(char* privatePath,string ssn,string name){
//string pub_voter_dir = "./DB/public/"+ssn;
char voter_dir_char[MAX_STR_SIZE];
clear_buff(voter_dir_char, MAX_STR_SIZE);
strcat(voter_dir_char,"./DB/public/" );
strcat(voter_dir_char, ssn.c_str());
// RSA* key_voter = getPublicKey(voter_dir_char);
RSA* key_CA = getPrivateKey(privatePath);
int* public_length = new int(0);
// char* vote_public_key = get_public_key(key_voter,public_length);
int* encrypt_len = new int(0);
char voter_name[MAX_STR_SIZE];
clear_buff(voter_name, MAX_STR_SIZE);
strcat(voter_name, name.c_str());
char* encrypted = encrypt_massage_with_private_key(key_CA,voter_name , encrypt_len);
if(encrypted==NULL){
cout << "error in encrypting voter public key!" << endl;
return false;
}
clear_buff(voter_dir_char, MAX_STR_SIZE);
strcat(voter_dir_char,"./DB/certificate/" );
strcat(voter_dir_char, ssn.c_str());
ofstream file (voter_dir_char);
if(file.is_open()){
file << encrypted;
file.close();
cout << "certificate create successfully!" << endl;
return true;
}
cout<<"error in creating certificate" << endl;
return false;
}
/*
int public_key_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted)
{
cerr << "ecrypting data is:\n" << enc_data << endl;
cerr << "key is :\n" << key << endl;
RSA * rsa = createRSA(key,1);
cerr << "after create RSA" << endl;
int result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,RSA_PKCS1_PADDING);
cerr << "after decreptino result is: " << result << endl;
return result;
}
*/
/*
void decrypt_client_massage_contain_public_key(char* massage){
cerr << "massage is\n" << massage << endl;
vector<string> token = mytokenizer_char(massage,"#");
cerr << "token[1] is :" << token[1] << endl;
int massge_size= atoi( token[1].c_str() );
cerr << "before constructor: massage size is : " << massge_size << endl;
string encrypt(&massage[9+ numDigits(massge_size)+1],massge_size);
cerr <<"encrypt massage recieve is :" << encrypt << endl;
token = mytokenizer_char(&massage[256],"#");
cerr << "token[0] is :" << token[0] << endl;
int pub_key_size = atoi( token[0].c_str() );
string publicKey(&massage[256+ numDigits(pub_key_size)+2],pub_key_size);
cerr << "public key is:" << publicKey.c_str() << endl;
char decrypted [MAX_STR_SIZE];
clear_buff(decrypted, MAX_STR_SIZE);
if( public_key_decrypt((unsigned char * )&massage[9+ numDigits(massge_size)+1],massge_size,(unsigned char * )&massage[256+ numDigits(pub_key_size)+2],(unsigned char * )decrypted) ==-1)
cout << "error in decryption" << endl;
else
cout << "recieved decrypted massage is: " << decrypted << endl;
}
*/
char* get_public_key(RSA* keypair,int* public_length){
BIO *pub = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPublicKey(pub, keypair);
size_t pub_len = BIO_pending(pub);
*public_length= convert_size_t_to_int(pub_len);
char* pub_key = (char*)malloc(pub_len + 1);
BIO_read(pub, pub_key, pub_len);
pub_key[pub_len] = '\0';
BIO_free_all(pub);
return pub_key;
}
char* encrypt_massage_with_private_key(RSA*keypair,char* msg ,int* encrypt_len){
char* encrypt = new char[RSA_size(keypair)];
char*err = new char[130];
if((*encrypt_len = RSA_private_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt,
keypair, RSA_PKCS1_PADDING)) == -1) {
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error encrypting message: %s\n", err);
return NULL;
}
return encrypt;
}
char* decrypt_massage_with_public_key(RSA* keypair,char* encrypted,int encrypt_len){
char* decrypt =new char[encrypt_len];
char*err = new char[130];
if(RSA_public_decrypt(encrypt_len, (unsigned char*)encrypted, (unsigned char*)decrypt,
keypair, RSA_PKCS1_OAEP_PADDING) == -1) {
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error decrypting message: %s\n", err);
return NULL;
}
return decrypt;
}
/*
int main(void) {
size_t pri_len; // Length of private key
size_t pub_len; // Length of public key
char *pri_key; // Private key
char *pub_key; // Public key
char msg[KEY_LENGTH/8]; // Message to encrypt
char *encrypt = NULL; // Encrypted message
char *decrypt = NULL; // Decrypted message
char *err; // Buffer for any error messages
// Generate key pair
printf("Generating RSA (%d bits) keypair...", KEY_LENGTH);
fflush(stdout);
RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
// To get the C-string PEM form:
BIO *pri = BIO_new(BIO_s_mem());
BIO *pub = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_RSAPublicKey(pub, keypair);
pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);
pri_key = (char*)malloc(pri_len + 1);
pub_key = (char*)malloc(pub_len + 1);
BIO_read(pri, pri_key, pri_len);
BIO_read(pub, pub_key, pub_len);
pri_key[pri_len] = '\0';
pub_key[pub_len] = '\0';
#ifdef PRINT_KEYS
printf("\n%s\n%s\n", pri_key, pub_key);
#endif
printf("done.\n");
// Get the message to encrypt
printf("Message to encrypt: ");
fgets(msg, KEY_LENGTH-1, stdin);
msg[strlen(msg)-1] = '\0';
encrypt = (char*)malloc(RSA_size(keypair));
int encrypt_len;
err = (char*)malloc(130);
if((encrypt_len = RSA_public_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt,
keypair, RSA_PKCS1_OAEP_PADDING)) == -1) {
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error encrypting message: %s\n", err);
}
#ifdef WRITE_TO_FILE
// Write the encrypted message to a file
FILE *out = fopen("out.bin", "w");
fwrite(encrypt, sizeof(*encrypt), RSA_size(keypair), out);
fclose(out);
printf("Encrypted message written to file.\n");
free(encrypt);
encrypt = NULL;
// Read it back
printf("Reading back encrypted message and attempting decryption...\n");
encrypt =(char*) malloc(RSA_size(keypair));
out = fopen("out.bin", "r");
fread(encrypt, sizeof(*encrypt), RSA_size(keypair), out);
fclose(out);
#endif
// Decrypt it
decrypt =(char*) malloc(encrypt_len);
if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt,
keypair, RSA_PKCS1_OAEP_PADDING) == -1) {
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error decrypting message: %s\n", err);
// goto free_stuff;
}
printf("Decrypted message: %s\n", decrypt);
// free_stuff:
RSA_free(keypair);
BIO_free_all(pub);
BIO_free_all(pri);
free(pri_key);
free(pub_key);
free(encrypt);
free(decrypt);
free(err);
return 0;
}
*/