-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdmain.cpp
183 lines (134 loc) · 4.68 KB
/
oldmain.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
void handleErrors(void){
printf("Nothing Personal Kid.");
ERR_print_errors_fp(stderr);
abort();
// TODO managing freeing memory
}
//______________________________________________________________________________
int HOWL_BIO_to_char(
BIO* bp,
char** key){
// variables
int length;
// allocate key
length = BIO_pending(bp);
//*key = (char*) calloc(length + 1, 1);
*key = (char*) malloc(sizeof(char) * (length + 1));
if(!(BIO_read(bp, (unsigned char*) *key, length)))
handleErrors();
//std::cout << "CONVERTING KEY: " << length << std::endl;
//std::cout << strlen(*key) << std::endl;
//std::cout << *key << std::endl << std::endl;
return 1;
}
int HOWL_rsa_generate(char** publicKey, char** privateKey){
// variables
BIGNUM* big_num; // e
RSA* rsa;
BIO* bp_public;
BIO* bp_private;
// initialize the big number
if(!(big_num = BN_new()))
handleErrors();
if(!(BN_set_word(big_num, SSE)))
handleErrors();
// initialize the rsa structure and generate the key
if(!(rsa = RSA_new()))
handleErrors();
// generate private and public keys
if(!(RSA_generate_multi_prime_key(rsa, RSA_BITS, RSA_PRIMES, big_num, NULL)))
handleErrors();
// store the public key and private key
bp_public = BIO_new(BIO_s_mem());
if(!(PEM_write_bio_RSAPublicKey(bp_public, rsa)))
handleErrors();
bp_private = BIO_new(BIO_s_mem());
if(!(PEM_write_bio_RSAPrivateKey(bp_private, rsa, NULL, NULL, 0, NULL, NULL)))
handleErrors();
HOWL_BIO_to_char(bp_public, publicKey);
HOWL_BIO_to_char(bp_private, privateKey);
// free
BN_free(big_num);
RSA_free(rsa);
BIO_free(bp_private);
BIO_free(bp_public);
return 1;
}
int HOWL_rsa_encrypt(BIO* bp_public, char* plaintext, char** cyphertext){
// variables
RSA* rsa = NULL;
// create RSA
if(!(PEM_read_bio_RSAPublicKey(bp_public, &rsa, 0, 0)))
handleErrors();
*cyphertext = (char*) malloc(sizeof(char*) * 10000);
// encrypt
if(!(RSA_public_encrypt(
strlen(plaintext),
(unsigned char*) plaintext,
(unsigned char*) *cyphertext,
rsa,
RSA_PKCS1_PADDING)))
handleErrors();
return 1;
}
int HOWL_rsa_decrypt(BIO* bp_private, char* cyphertext, char** plaintext){
// variables
RSA* rsa = NULL;
// create RSA
if(!(PEM_read_bio_RSAPrivateKey(bp_private, &rsa, 0, 0)))
handleErrors();
*plaintext = (char*) malloc(sizeof(char*) * 10000);
// decrypt
if(!(RSA_private_decrypt(
strlen((char *) cyphertext),
(unsigned char*) cyphertext,
(unsigned char*) *plaintext,
rsa,
RSA_PKCS1_PADDING)))
handleErrors();
return 1;
}
//______________________________________________________________________________
m
int main(int argc, char *argv[]) {
// constants
char* userAPublic = NULL;
char* userAPrivate = NULL;
char* userBPublic = NULL;
char* userBPrivate = NULL;
howl::BlockChain* userA = NULL;
howl::BlockChain* userB = NULL;
openSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
// generate pair keys
//HOWL_rsa_generate(&userAPublic, &userAPrivate);
HOWL_rsa_generate(&userBPublic, &userBPrivate);
//std::cout << userBPublic << std::endl;
//std::cout << userBPrivate << std::endl;
////////////////////////////////////////////////////////////////////////////
userA = new howl::BlockChain((char *) "af13e92d44b1ac31");
userB = new howl::BlockChain((char *) "af13e92d44b1ac31");
std::cout << std::endl << "DISPLAY GENISIS BLOCK:" << std::endl;
std::cout << userA->toString() << std::endl;
std::cout << std::endl << "SEND GENISIS TO OTHER:" << std::endl;
char* temp1 = userA->getEncryptedBlock(userBPublic);
userB->addReceivedBlock(temp1, userBPrivate);
////////////////////////////////////////////////////////////////////////////
userA->buildSentBlock((char *) "message 1");
std::cout << std::endl << "DISPLAY FIRST MESSAGE:" << std::endl;
std::cout << userA->toString() << std::endl;
std::cout << std::endl << "SEND MESSAGE TO OTHER:" << std::endl;
char* temp2 = userA->getEncryptedBlock(userBPublic);
userB->addReceivedBlock(temp2, userBPrivate);
//khh
std::cout << std::endl << "Nothing Personal Kid." << std::endl;
ERR_print_errors_fp(stderr);
//free(userAPublic);
//free(userAPrivate);
//free(userBPublic);
//free(userBPrivate);
//free(userA);
//free(userB);
return 1;
}