AES Encryption Using Crypto++ .lib in Visual Studio C++
This is a quick note showing how to compile, link and include a Crypto++ static library (cryptlib.lib), compile and execute a sample code that uses AES CBC to encrypt and decrypt some string data.

Compiling cryptlib.lib

Open the crypto++ solution file cryptest.sln:
Change cryptlib project runtime library to Multi-threaded and change configuration to Release x64:
Build cryptlib project. It will spit out a cryptlib.lib static library:
1
C:\Users\mantvydas\Desktop\cryptopp\x64\Output\Release\cryptlib.lib
Copied!

Including cryptlib.lib in a Project

Create a new VS project and include cryptlib.lib that you've just compiled:
Change project's runtime library to Multi-threaded - it has to use the same runtime library as cryptlib.lib:
Copy over all the header files from the crypto++ project to your project's folder like so:
Include those headers in the project by adding the folder to Include Directories list:
Copy over the below sample code to your main .cpp file and compile:
crypto.cpp
1
// code copy pasted from here https://www.cryptopp.com/w/images/b/bd/AES-CBC-Filter.zip
2
// crypto.cpp : This file contains the 'main' function. Program execution begins and ends there.
3
//
4
5
#include "pch.h"
6
#include <iostream>
7
#include "aes.h"
8
#include <Windows.h>
9
10
#include "osrng.h"
11
using CryptoPP::AutoSeededRandomPool;
12
13
#include <iostream>
14
using std::cout;
15
using std::cerr;
16
using std::endl;
17
18
#include <string>
19
using std::string;
20
21
#include <cstdlib>
22
using std::exit;
23
24
#include "cryptlib.h"
25
using CryptoPP::Exception;
26
27
#include "hex.h"
28
using CryptoPP::HexEncoder;
29
using CryptoPP::HexDecoder;
30
31
#include "filters.h"
32
using CryptoPP::StringSink;
33
using CryptoPP::StringSource;
34
using CryptoPP::StreamTransformationFilter;
35
36
#include "aes.h"
37
using CryptoPP::AES;
38
39
#include "ccm.h"
40
using CryptoPP::CBC_Mode;
41
42
#include "assert.h"
43
44
int main(int argc, char* argv[])
45
{
46
AutoSeededRandomPool prng;
47
48
byte key[AES::DEFAULT_KEYLENGTH];
49
prng.GenerateBlock(key, sizeof(key));
50
51
byte iv[AES::BLOCKSIZE];
52
prng.GenerateBlock(iv, sizeof(iv));
53
54
string plain = "CBC Mode Test";
55
string cipher, encoded, recovered;
56
57
/*********************************\
58
\*********************************/
59
60
// Pretty print key
61
encoded.clear();
62
StringSource(key, sizeof(key), true,
63
new HexEncoder(
64
new StringSink(encoded)
65
) // HexEncoder
66
); // StringSource
67
cout << "key: " << encoded << endl;
68
69
// Pretty print iv
70
encoded.clear();
71
StringSource(iv, sizeof(iv), true,
72
new HexEncoder(
73
new StringSink(encoded)
74
) // HexEncoder
75
); // StringSource
76
cout << "iv: " << encoded << endl;
77
78
/*********************************\
79
\*********************************/
80
81
try
82
{
83
cout << "plain text: " << plain << endl;
84
85
CBC_Mode< AES >::Encryption e;
86
e.SetKeyWithIV(key, sizeof(key), iv);
87
88
// The StreamTransformationFilter removes
89
// padding as required.
90
StringSource s(plain, true,
91
new StreamTransformationFilter(e,
92
new StringSink(cipher)
93
) // StreamTransformationFilter
94
); // StringSource
95
96
#if 0
97
StreamTransformationFilter filter(e);
98
filter.Put((const byte*)plain.data(), plain.size());
99
filter.MessageEnd();
100
101
const size_t ret = filter.MaxRetrievable();
102
cipher.resize(ret);
103
filter.Get((byte*)cipher.data(), cipher.size());
104
#endif
105
}
106
catch (const CryptoPP::Exception& e)
107
{
108
cerr << e.what() << endl;
109
exit(1);
110
}
111
112
/*********************************\
113
\*********************************/
114
115
// Pretty print
116
encoded.clear();
117
StringSource(cipher, true,
118
new HexEncoder(
119
new StringSink(encoded)
120
) // HexEncoder
121
); // StringSource
122
cout << "cipher text: " << encoded << endl;
123
124
/*********************************\
125
\*********************************/
126
127
try
128
{
129
CBC_Mode< AES >::Decryption d;
130
d.SetKeyWithIV(key, sizeof(key), iv);
131
132
// The StreamTransformationFilter removes
133
// padding as required.
134
StringSource s(cipher, true,
135
new StreamTransformationFilter(d,
136
new StringSink(recovered)
137
) // StreamTransformationFilter
138
); // StringSource
139
140
#if 0
141
StreamTransformationFilter filter(d);
142
filter.Put((const byte*)cipher.data(), cipher.size());
143
filter.MessageEnd();
144
145
const size_t ret = filter.MaxRetrievable();
146
recovered.resize(ret);
147
filter.Get((byte*)recovered.data(), recovered.size());
148
#endif
149
150
cout << "recovered text: " << recovered << endl;
151
}
152
catch (const CryptoPP::Exception& e)
153
{
154
cerr << e.what() << endl;
155
exit(1);
156
}
157
158
/*********************************\
159
\*********************************/
160
161
return 0;
162
}
Copied!
Success:

References

https://www.cryptopp.com/w/images/b/bd/AES-CBC-Filter.zip
Link errors using cryptopp on VS2012 static library, console application and clr program
Stack Overflow
Crypto++ Library 8.6 | Free C++ Class Library of Cryptographic Schemes
Last modified 2yr ago