-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_op.cc
190 lines (149 loc) · 5.11 KB
/
crypto_op.cc
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
/*
* Copyright (c) 2017-2018 SYZ
* g++ crypto_op.cc -std=c++11 -lcrypto -o release/crypto_op
*/
#include <openssl/des.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <string.h>
#include <string>
#include <vector>
#ifdef _WIN32
#include <Windows.h>
#include <winsock.h>
extern "C" {
#include <openssl/applink.c>
}
#else
#include <arpa/inet.h>
#endif
typedef enum {
GENERAL = 0,
ECB,
} CRYPTO_MODE;
std::string des_encrypt(const std::string &cleartext, const std::string &key);
std::string des_decrypt(const std::string &ciphertext, const std::string &key);
char *rsa_encrypt(const unsigned char *str, const char *public_key_filename,
int *len);
char *rsa_decrypt(const unsigned char *str, const char *private_key_filename,
int *len);
std::string des_encrypt(const std::string &cleartext, const std::string &key) {
std::string strCipherText;
CRYPTO_MODE mode = GENERAL;
switch (mode) {
case GENERAL:
case ECB: {
DES_cblock keyEncrypt;
memset(keyEncrypt, 0, 8);
if (key.length() <= 8)
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), 8);
DES_key_schedule keySchedule;
DES_set_key_unchecked(&keyEncrypt, &keySchedule);
const_DES_cblock inputText;
DES_cblock outputText;
std::vector<unsigned char> vecCiphertext;
unsigned char tmp[8];
for (int i = 0; i < cleartext.length() / 8; i++) {
memcpy(inputText, cleartext.c_str() + i * 8, 8);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_ENCRYPT);
memcpy(tmp, outputText, 8);
for (int j = 0; j < 8; j++) vecCiphertext.push_back(tmp[j]);
}
if (cleartext.length() % 8 != 0) {
int tmp1 = cleartext.length() / 8 * 8;
int tmp2 = cleartext.length() - tmp1;
memset(inputText, 0, 8);
memcpy(inputText, cleartext.c_str() + tmp1, tmp2);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_ENCRYPT);
memcpy(tmp, outputText, 8);
for (int j = 0; j < 8; j++) vecCiphertext.push_back(tmp[j]);
}
strCipherText.clear();
strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
} break;
}
return strCipherText;
}
std::string des_decrypt(const std::string &ciphertext, const std::string &key) {
std::string strClearText;
CRYPTO_MODE mode = GENERAL;
switch (mode) {
case GENERAL:
case ECB: {
DES_cblock keyEncrypt;
memset(keyEncrypt, 0, 8);
if (key.length() <= 8)
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), 8);
DES_key_schedule keySchedule;
DES_set_key_unchecked(&keyEncrypt, &keySchedule);
const_DES_cblock inputText;
DES_cblock outputText;
std::vector<unsigned char> vecCleartext;
unsigned char tmp[8];
for (int i = 0; i < ciphertext.length() / 8; i++) {
memcpy(inputText, ciphertext.c_str() + i * 8, 8);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_DECRYPT);
memcpy(tmp, outputText, 8);
for (int j = 0; j < 8; j++) vecCleartext.push_back(tmp[j]);
}
if (ciphertext.length() % 8 != 0) {
int tmp1 = ciphertext.length() / 8 * 8;
int tmp2 = ciphertext.length() - tmp1;
memset(inputText, 0, 8);
memcpy(inputText, ciphertext.c_str() + tmp1, tmp2);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_DECRYPT);
memcpy(tmp, outputText, 8);
for (int j = 0; j < 8; j++) vecCleartext.push_back(tmp[j]);
}
strClearText.clear();
strClearText.assign(vecCleartext.begin(), vecCleartext.end());
} break;
}
return strClearText;
}
char *rsa_encrypt(const unsigned char *str, const char *public_key_filename,
int *len) {
char *p_en = NULL;
RSA *p_rsa = NULL;
FILE *pf = NULL;
int rsa_len = 0;
do {
if ((pf = fopen(public_key_filename, "rb")) == NULL) break;
if ((p_rsa = PEM_read_RSA_PUBKEY(pf, NULL, NULL, NULL)) == NULL) break;
rsa_len = RSA_size(p_rsa);
p_en = static_cast<char *>(malloc(rsa_len + 1));
memset(p_en, 0, rsa_len + 1);
if ((*len = RSA_public_encrypt(rsa_len, str, (unsigned char *)p_en, p_rsa,
RSA_NO_PADDING)) < 0)
break;
} while (0);
RSA_free(p_rsa);
if (pf) fclose(pf);
return p_en;
}
char *rsa_decrypt(const unsigned char *str, const char *private_key_filename,
int *len) {
char *p_de = NULL;
RSA *p_rsa = NULL;
FILE *pf = NULL;
int rsa_len = 0;
do {
if ((pf = fopen(private_key_filename, "rb")) == NULL) break;
if ((p_rsa = PEM_read_RSAPrivateKey(pf, NULL, NULL, NULL)) == NULL) break;
rsa_len = RSA_size(p_rsa);
p_de = static_cast<char *>(malloc(rsa_len + 1));
memset(p_de, 0, rsa_len + 1);
if ((*len = RSA_private_decrypt(rsa_len, str, (unsigned char *)p_de, p_rsa,
RSA_NO_PADDING)) < 0)
break;
} while (0);
RSA_free(p_rsa);
if (pf) fclose(pf);
return p_de;
}
int main(int argc, char *argv[]) { return 0; }