0%

compile and install cryptopp 7.0 on windows 10

Guide

  • windows 10 64-bit
  • crypto++ 7.0
  • vs 2015

compile

download crypto++ 7.0

open cryptest.sln with Visual Studio 2015

change Windows SDK Version from 8.1 to 10.0

png

compile cryptodll with Release x64 and we get cryptopp.dll and cryptopp.lib
png

sdk

copy headers to include, copy libs to lib and dlls to dll like this:

png

cryptopp-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set(CRYPTOPP_FOUND TRUE) # auto 
set(CRYPTOPP_VERSION 7.0.0)
set(CRYPTOPP_ROOT_DIR "C:/car_libs/cryptopp-7.0")

find_path(CRYPTOPP_INCLUDE_DIR NAMES cryptopp/aes.h PATHS "${CRYPTOPP_ROOT_DIR}/include")
mark_as_advanced(CRYPTOPP_INCLUDE_DIR) # show entry in cmake-gui

find_library(CRYPTOPP_LIBRARY NAMES cryptopp.lib PATHS "${CRYPTOPP_ROOT_DIR}/lib")
mark_as_advanced(CRYPTOPP_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(CRYPTOPP_INCLUDE_DIRS ${CRYPTOPP_INCLUDE_DIR} )
set(CRYPTOPP_LIBRARIES ${CRYPTOPP_LIBRARY} )

message( "cryptopp-config.cmake " ${CRYPTOPP_ROOT_DIR})

Example

see cryptopp-7.0/dlltest.cpp

Code

cipher.h

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
#pragma once

#pragma warning( disable: 4275 )
#pragma warning( disable: 4819 )

#include <iostream>

#include <cryptopp/dll.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/modes.h>

using namespace std;

USING_NAMESPACE(CryptoPP)

class CipherApiImpl
{
public:
enum MODE
{
AES,
DES
};

public:
CipherApiImpl(MODE mode = MODE::AES);

int get_filesize(const char *filename);

int save(const std::string& filepath, const std::string& str);
std::string load(const std::string& filepath);

std::string encrypt_string(const std::string& plain_text);
std::string decrypt_string(const std::string& cipher_text);

std::string encrypt_file(const std::string& infile);
std::string decrypt_file(const std::string& infile);

int encrypt_file(const std::string& infile, const std::string& outfile);
int decrypt_file(const std::string& infile, const std::string& outfile);

protected:
std::string tohex(const char& c);
std::string tohexs(const std::string& str);

char tochar(const char& hex1, const char& hex2);
std::string tochars(const std::string& hex);

private:
// 32 + 16
const byte aes_key[CryptoPP::AES::MAX_KEYLENGTH] = {
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
};
const byte aes_iv[CryptoPP::AES::BLOCKSIZE] = {
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
};

// 24+8
const byte des_key[CryptoPP::DES_EDE3::MAX_KEYLENGTH] = {
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
};
const byte des_iv[CryptoPP::DES_EDE3::BLOCKSIZE] = {
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
};

CryptoPP::CFB_FIPS_Mode<CryptoPP::AES>::Encryption aes_encryption;
CryptoPP::CFB_FIPS_Mode<CryptoPP::AES>::Decryption aes_decryption;

CryptoPP::CFB_FIPS_Mode<CryptoPP::DES_EDE3>::Encryption des_encryption;
CryptoPP::CFB_FIPS_Mode<CryptoPP::DES_EDE3>::Decryption des_decryption;

MODE m_mode;
};

cipher.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
#include "cipher_api_impl.h"

#include <sys/stat.h>

CipherApiImpl::CipherApiImpl(MODE mode)
{
m_mode = mode;

// for des
des_encryption.SetKeyWithIV(des_key, sizeof(des_key), des_iv);
des_decryption.SetKeyWithIV(des_key, sizeof(des_key), des_iv);

// for aes
aes_encryption.SetKeyWithIV(aes_key, sizeof(aes_key), aes_iv);
aes_decryption.SetKeyWithIV(aes_key, sizeof(aes_key), aes_iv);
}

#pragma region hex/char

std::string CipherApiImpl::tohex(const char& c)
{
/*
00,01,02,...0F [0,1,2,...15]
10,11,12,...1F [16,17,...31]
...
F0,F1,F2,...FF [240,241,...255]
*/

/*
char ch[2] = { 0 };
sprintf(ch, "%02x", static_cast<byte>(c)); // char ---> 16 hex
return std::string(ch);
*/
const char HEX[] = "0123456789ABCDEF";
const int HEX_BASE = 16;

std::string c_hex;

int uc = (unsigned char)(c);
int a = uc / HEX_BASE;
int b = uc % HEX_BASE;
c_hex.push_back(HEX[a]);
c_hex.push_back(HEX[b]);

return c_hex;
}

std::string CipherApiImpl::tohexs(const std::string& str)
{
const char HEX[] = "0123456789ABCDEF";
const int HEX_BASE = 16;

std::string hex_result;
for (size_t i = 0; i < str.size(); i++)
{
std::string c_hex;
int uc = (unsigned char)(str[i]);
int a = uc / HEX_BASE;
int b = uc % HEX_BASE;
c_hex.push_back(HEX[a]);
c_hex.push_back(HEX[b]);

hex_result += c_hex;
}
return hex_result;
}

char CipherApiImpl::tochar(const char& hex1, const char& hex2)
{
const int HEX_BASE = 16;

int h1 = (int)hex1;
int h2 = (int)hex2;

// F1 ===> 16,1===>15*16+1 = 241 ===> char
char uc = (char)(h1*HEX_BASE + h2);
return uc;
}

std::string CipherApiImpl::tochars(const std::string& hex)
{
if (hex.size() % 2 != 0)
{
return "";
};

std::map<char, int> hex_int = {
{ '0' , 0 },
{ '1', 1 },
{ '2', 2 },
{ '3', 3 },
{ '4', 4 },
{ '5', 5 },
{ '6', 6 },
{ '7', 7 },
{ '8', 8 },
{ '9', 9 },
{ 'A', 10 },
{ 'B', 11 },
{ 'C', 12 },
{ 'D', 13 },
{ 'E', 14 },
{ 'F', 15 },
};

std::string char_result;
const int HEX_BASE = 16;

for (size_t i = 0; i < hex.size(); i += 2)
{
int h1 = hex_int.at(hex[i]); // 'F' ===>15
int h2 = hex_int.at(hex[i + 1]); // '1'===>1

// F1 ===> 15,1===>15*16+1 = 241 ===> char
//std::cout << "===============\n";
char uc = (char)(h1*HEX_BASE + h2);
//std::cout << h1 <<","<<h2 << std::endl;
//std::cout << uc << std::endl;
char_result += uc;
}
return char_result;
}
#pragma endregion

#pragma region encrypt_string and decrypt_string

int CipherApiImpl::get_filesize(const char *filename)
{
struct stat f_stat;
if (stat(filename, &f_stat) == -1)
{
return -1;
}
return f_stat.st_size;
}

/*
std::string load_text_not_use(const std::string& filepath)
{
std::string str;
ifstream in(filepath, ios::in | ios::binary);

std::string line;
std::string content;
while (getline(in, line))
{
//std::cout << "line.size()=" << line.size() << std::endl;
if (line.size() > 0) {
content += line + "\n";
}
line.clear();
}

in.close();

return content;
}
*/

int CipherApiImpl::save(const std::string& filepath, const std::string& str)
{
ofstream out(filepath, ios::out | ios::binary); // binary mode (default text mode)
out.write(str.c_str(), str.length());
out.close();
return 1;
}

std::string CipherApiImpl::load(const std::string& filepath)
{
int filesize = get_filesize(filepath.c_str());

char *buffer = new char[filesize];

ifstream in(filepath, ios::in | ios::binary); // binary mode (default text mode)
in.read(buffer, filesize);
in.close();

// std::string content(buffer); // ERROR for binary file
std::string content;
for (size_t i = 0; i < filesize; i++)
{
content.push_back(buffer[i]);
}
delete[] buffer;

return content;
}

/*
//==========================================================================
std::string cipher_text;
CryptoPP::AES::CipherApiImpl aesEncryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::CipherApiImpl cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(cipher_text));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plain_text.c_str()), plain_text.length());
stfEncryptor.MessageEnd();

std::string cipher_text_hex = tohexs(cipher_text);
return cipher_text_hex;
//==========================================================================
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedText));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipher_text.c_str()), cipher_text.size());
stfDecryptor.MessageEnd();

return decrypted_text;
//==========================================================================

byte ciphertext[24];
byte decrypted[24];

CFB_FIPS_Mode<DES_EDE3>::Encryption encryption_DES_EDE3_CFB;
encryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);
encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);

CFB_FIPS_Mode<DES_EDE3>::Decryption decryption_DES_EDE3_CFB;
decryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);
decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);
*/

std::string CipherApiImpl::encrypt_string(const std::string& plain_text)
{
size_t size = plain_text.size();
const byte* in_byte = (const unsigned char*)(plain_text.c_str());

byte* out_byte = new byte[size];

des_encryption.ProcessString(out_byte, in_byte, size);

std::string cipher_text;
for (size_t i = 0; i < size; i++)
{
cipher_text.push_back(out_byte[i]);
}
delete[] out_byte;

return cipher_text;
}

std::string CipherApiImpl::decrypt_string(const std::string& cipher_text)
{
size_t size = cipher_text.size();
const byte* in_byte = (const unsigned char*)(cipher_text.c_str());
byte* out_byte = new byte[size];

des_decryption.ProcessString(out_byte, in_byte, size);

std::string plain_text;
for (size_t i = 0; i < size; i++)
{
plain_text.push_back(out_byte[i]);
}
delete[] out_byte;

return plain_text;
}

std::string CipherApiImpl::encrypt_file(const std::string& infile)
{
std::string plain_text = load(infile);
std::string cipher_text = encrypt_string(plain_text);
return cipher_text;
}

std::string CipherApiImpl::decrypt_file(const std::string& infile)
{
std::string cipher_text = load(infile);
std::string plain_text = decrypt_string(cipher_text);
return plain_text;
}

int CipherApiImpl::encrypt_file(const std::string& infile, const std::string& outfile)
{
std::string cipher_text_hex = encrypt_file(infile);
return save(outfile, cipher_text_hex);
}

int CipherApiImpl::decrypt_file(const std::string& infile, const std::string& outfile)
{
std::string plain_text = decrypt_file(infile);
return save(outfile, plain_text);
}

#pragma endregion

test.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
#include <iostream>
#include <string>

#include <gtest/gtest.h>
#include <glog/logging.h>

#pragma warning( disable: 4819 )
#pragma warning( disable: 4244 )
#pragma warning( disable: 4267 )

#include "cipher/cipher.h"

using namespace std;

int test_cipher_api()
{
CipherApi cipher;

std::string text = "[hello zhuzhu dashen !]\n[1234567]\n[123]\n1\n";
cout << "text : " << text << endl;
cout << "text size: " << text.size() << endl;
cipher.save("./tmp/text.txt", text);

std::cout << "==============encrypt/decrypt string======================\n";

std::string cipherHex = cipher.encrypt_string(text);
//cout << "cipher : " << cipherHex << endl;
cipher.save("./tmp/cipherHex.txt", cipherHex);

std::string text2 = cipher.decrypt_string(cipherHex);
cout << "text2 : " << text2 << endl;
cout << "text2 size: " << text2.size() << endl;
cipher.save("./tmp/text2.txt", text2);

// load_text
std::string text_load = cipher.load("./tmp/text2.txt");
cout << "text_load : " << text_load << endl;
cout << "text_load size: " << text_load.size() << endl;

// encrypt/decrypt text file
std::cout << "==============encrypt/decrypt text file======================\n";
cipher.encrypt_file("./tmp/text.txt", "./tmp/text_encryption.txt");
cipher.decrypt_file("./tmp/cipherHex.txt", "./tmp/cipherHex_decryption.txt");

// encrypt/decrypt prototxt
std::cout << "==============encrypt/decrypt prototxt======================\n";
cipher.encrypt_file("./tmp/small/deploy.prototxt", "./tmp/small/deploy_encryption.prototxt");
cipher.decrypt_file("./tmp/small/deploy_encryption.prototxt", "./tmp/small/deploy_new.prototxt");

std::cout << "==============encrypt/decrypt caffemodel======================\n";
cipher.encrypt_file("./tmp/small/recon__iter_140000.caffemodel", "./tmp/small/recon__iter_140000_encryption.caffemodel");
cipher.decrypt_file("./tmp/small/recon__iter_140000_encryption.caffemodel", "./tmp/small/recon__iter_140000_new.caffemodel");
return 0;
}

TEST(cipher_test, test_cipher_api) {
test_cipher_api();
}

CMakeLists.txt

1
2
3
4
5
6
7
find_package(CRYPTOPP REQUIRED)

MESSAGE( [Main] " CRYPTOPP_INCLUDE_DIRS = ${CRYPTOPP_INCLUDE_DIRS}")
MESSAGE( [Main] " CRYPTOPP_LIBRARIES = ${CRYPTOPP_LIBRARIES}")

include_directories(${CRYPTOPP_INCLUDE_DIRS})
target_link_libraries (demo ${CRYPTOPP_LIBRARIES})

Reference

History

  • 20181018: created.