-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBase64.hpp
More file actions
240 lines (194 loc) · 4.06 KB
/
Base64.hpp
File metadata and controls
240 lines (194 loc) · 4.06 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
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
#ifndef ENCRYPT_BASE64_HPP
#define ENCRYPT_BASE64_HPP
#include <string>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include "FileIO.hpp"
OPEN_NAMESPACE_SWITCHTOOL
using namespace std;
class EncryptBase64
{
public:
EncryptBase64(void)
{
m_pPlainText = NULL;
m_lPlanLen = 0;
m_pCipherText = NULL;
m_lCipherLen = 0;
m_pFileIO = NULL;
}
int SetPlainText(const unsigned char* pData, const unsigned long len)
{
if (pData == NULL)
{
return -1;
}
if (m_pPlainText)
{
free(m_pPlainText);
}
m_pPlainText = (unsigned char*)malloc(len);
memcpy(m_pPlainText, pData, len);
m_lPlanLen = len;
return 0;
}
int SetPlainText(const char* filename)
{
int ret;
unsigned char* pData;
unsigned long len;
if (filename == NULL) {
return -1;
}
if (m_pFileIO) {
delete m_pFileIO;
}
m_pFileIO = new EncryptFileIO(filename);
ret = m_pFileIO->Read();
if (ret != 0) {
return -1;
}
ret = m_pFileIO->GetData(&pData, &len);
if (ret != 0) {
return -1;
}
return SetPlainText(pData, len);
}
int SetCipherText(const unsigned char* pData, const unsigned long len)
{
if (pData == NULL)
{
return -1;
}
if (m_pCipherText)
{
free(m_pCipherText);
}
m_pCipherText = (unsigned char*)malloc(len);
memcpy(m_pCipherText, pData, len);
m_lCipherLen = len;
return 0;
}
int SetCipherText(const char* filename)
{
int ret;
unsigned char* pData;
unsigned long len;
if (filename == NULL) {
return -1;
}
if (m_pFileIO) {
delete m_pFileIO;
}
m_pFileIO = new EncryptFileIO(filename);
ret = m_pFileIO->Read();
if (ret != 0) {
return -1;
}
ret = m_pFileIO->GetData(&pData, &len);
if (ret != 0) {
return -1;
}
return SetCipherText(pData, len);
}
int Encode(void)
{
if (m_pPlainText == NULL)
{
return -1;
}
BIO * bmem = NULL;
BIO * b64 = NULL;
BUF_MEM * bptr = NULL;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, m_pPlainText, m_lPlanLen);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
if (m_pCipherText)
{
free(m_pCipherText);
}
m_pCipherText = (unsigned char *)malloc(bptr->length);
memcpy(m_pCipherText, bptr->data, bptr->length);
m_lCipherLen = bptr->length;
BIO_free_all(b64);
return 0;
}
int Decode(void)
{
if (m_pCipherText == NULL)
{
return -1;
}
BIO * b64 = NULL;
BIO * bmem = NULL;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new_mem_buf(m_pCipherText, m_lCipherLen);
b64 = BIO_push(b64, bmem);
if (m_pPlainText)
{
free(m_pPlainText);
}
m_pPlainText = (unsigned char*)malloc(m_lCipherLen);
m_lPlanLen = BIO_read(b64, m_pPlainText, m_lCipherLen);
BIO_free_all(b64);
return 0;
}
int GetPlainText(unsigned char** ppData, unsigned long* pLen)
{
if (ppData == NULL || pLen == NULL || m_pPlainText == NULL)
{
return -1;
}
*ppData = m_pPlainText;
*pLen = m_lPlanLen;
return 0;
}
int GetCipherText(unsigned char** ppData, unsigned long* pLen)
{
if (ppData == NULL || pLen == NULL || m_pCipherText == NULL)
{
return -1;
}
*ppData = m_pCipherText;
*pLen = m_lCipherLen;
return 0;
}
int WritePlainText2File(const char* filename)
{
if (filename == NULL || m_pPlainText == NULL) {
return -1;
}
EncryptFileIO io(filename);
io.SetData(m_pPlainText, m_lPlanLen);
return io.Write();
}
int WriteCipherText2File(const char* filename)
{
if (filename == NULL || m_pCipherText == NULL) {
return -1;
}
EncryptFileIO io(filename);
io.SetData(m_pCipherText, m_lCipherLen);
return io.Write();
}
~EncryptBase64(void)
{
free(m_pPlainText);
free(m_pCipherText);
delete m_pFileIO;
}
private:
unsigned char* m_pPlainText; // 明文
unsigned long m_lPlanLen; // 明文长度
unsigned char* m_pCipherText; // 密文
unsigned long m_lCipherLen; // 密文长度
EncryptFileIO* m_pFileIO;
};
CLOSE_NAMESPACE_SWITCHTOOL
#endif // ENCRYPT_BASE64_HPP