-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
169 lines (134 loc) · 5.05 KB
/
main.cpp
File metadata and controls
169 lines (134 loc) · 5.05 KB
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
#include <iostream>
#include "CBigInteger.h"
#include <openssl/rsa.h>
bool RSA_key_gen(const uint32_t key_bits, std::string& modulus_str, std::string& pub_str, std::string& priv_str)
{
unsigned char key_buf[256];
if(key_bits/CHAR_BIT > sizeof(key_buf))
return false;
RSA *r = RSA_new();
BIGNUM *bne = BN_new();
unsigned long e = RSA_F4; // 0x10001
//--------generate rsa keys-------------------------------------------------------------
int ret = BN_set_word(bne,e);
if(ret != 1){
RSA_free(r);
BN_free(bne);
return false;
}
ret = RSA_generate_key_ex(r, key_bits, bne, NULL);
if(ret != 1){
RSA_free(r);
BN_free(bne);
return false;
}
//--------keys to BIGNUM----------------------------------------------------------------
const BIGNUM *bmodulus, *bpub, *bpriv;
RSA_get0_key(r, &bmodulus, &bpub, &bpriv);
//--------keys to std::string-----------------------------------------------------------
int bn_size = BN_bn2bin(bmodulus, &key_buf[0]);
if(bn_size != 0)
modulus_str = std::string((const char*)&key_buf[0], bn_size);
else
return false;
bn_size = BN_bn2bin(bpub, &key_buf[0]);
if(bn_size != 0)
pub_str = std::string((const char*)&key_buf[0], bn_size);
else
return false;
bn_size = BN_bn2bin(bpriv, &key_buf[0]);
if(bn_size != 0)
priv_str = std::string((const char*)&key_buf[0], bn_size);
else
return false;
RSA_free(r);
BN_free(bne);
return true;
}
void printStrAsHexByte(std::string label, std::string str)
{
std::cout << label << " length = " << std::dec << str.length() << std::endl;
for(int i=0; i< str.length(); i++)
{
if(i!=0 && i%16 == 0)
std::cout << std::endl;
int hex = str[i]&0xff;
std::cout << " 0x" << std::hex << hex;
}
std::cout << std::endl;
}
bool RSA_public_encrypt(const std::string& message_str, const std::string& e_str, const std::string& modulus_str, std::string& ciphertext_str)
{
unsigned char ciphertext_buf[256];
if(sizeof(ciphertext_buf) < modulus_str.size())
return false;
CData e, message, modulus, ciphertext;
FromBytesArray( message, (const unsigned char*)message_str.c_str(), message_str.length());
FromBytesArray( modulus, (const unsigned char*)modulus_str.c_str(), modulus_str.length());
FromBytesArray( e, (const unsigned char*)e_str.c_str(), e_str.length());
/*
message.TraceAsHex();
modulus.TraceAsHex();
e.TraceAsHex();
*/
// encrypt
ModPow( ciphertext, message, e, modulus);
ToBytesArray( ciphertext_buf, ciphertext, modulus_str.size());
ciphertext_str = std::string((const char*)ciphertext_buf, modulus_str.size());
return true;
}
bool RSA_private_decrypt(const std::string& ciphertext_str, const std::string& d_str, const std::string& modulus_str, std::string& message_str)
{
unsigned char message_buf[256];
if(sizeof(message_buf) < modulus_str.size())
return false;
CData ciphertext, d, message, modulus;
FromBytesArray( ciphertext, (const unsigned char*)ciphertext_str.c_str(), ciphertext_str.length());
FromBytesArray( modulus, (const unsigned char*)modulus_str.c_str(), modulus_str.length());
FromBytesArray( d, (const unsigned char*)d_str.c_str(), d_str.length());
/*
ciphertext.TraceAsHex();
modulus.TraceAsHex();
d.TraceAsHex();
*/
// decrypt
ModPow(message, ciphertext, d, modulus );
uint32_t message_size = message.m_iDataSize*4;
ToBytesArray( message_buf, message, message_size);
message_str = std::string((const char*)message_buf, message_size);
return true;
}
bool myTest()
{
std::string e_str, d_str, modulus_str, ciphertext_str, message2_str;
uint32_t key_bits = 256;
bool result = RSA_key_gen(key_bits, modulus_str, e_str, d_str);
if(result == false)
return result;
const unsigned char message[] = {0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11};
std::string message_str = std::string((const char*)&message[0], sizeof(message));
printStrAsHexByte("modulus_str", modulus_str);
printStrAsHexByte("e_str", e_str);
printStrAsHexByte("d_str", d_str);
printStrAsHexByte("message_str", message_str);
result = RSA_public_encrypt(message_str, e_str, modulus_str, ciphertext_str);
if(result == false)
return result;
printStrAsHexByte("ciphertext_str", ciphertext_str);
result = RSA_private_decrypt(ciphertext_str, d_str, modulus_str, message2_str);
if(result == false)
return result;
printStrAsHexByte("cmessage2_str", message2_str);
if(message_str == message2_str)
result = true;
return result;
}
int main(int, char**) {
std::cout << "Hello, RSA!\n";
bool res = myTest();
if(res == true)
std::cout << "Test OK!" << std::endl;
else
std::cout << "Test OK!" << std::endl;
}