Monday, April 15, 2013

Compiling and Integrating Crypto++ into the Microsoft Visual C++ Environment + Running Sample program

Compiling and Integrating Crypto++ into the Microsoft Visual C++

Step 1- Download the latest Crypto++ library from the link And unzip the file in a folder.

Step 2- Go to the extracted folder and open the cryptest Solution file with visual studio.(see the picture below)

Step 3- You will find four projects in the solution file shown in the picture.

Step 4- Go to Build tab in the ManuBar and select Batch Build.

Step 5- In the batch build Select the option Selected in the picture and then Build the Solution .

This will generate the Two folders in the Win32 ->Output Folders .One ‘Debug’ and named ‘Release’.

The ‘Debug ’ folder contains the following files

And The ‘Release’ folder contains the following files 

Step 6- Now Make A new visual C++ win32 project and Name it According to your self
Then go to the Project properties by right clicking your project. (See the picture)

Now go to the Linker and the Select the Input and in the Additional Dependency Enter the cryptlib.lib path as shown in the picture. If you are compiling the project in ‘Release’ mode the n select the cryptlib.lib file in your Release Folder and if you are compiling the project in ‘Debug’ mode the n select the cryptlib.lib file from your Debug Folder.

Then Enter the Sample Code shown Below

#include "stdafx.h"

// g++ -g3 -ggdb -O0 -DDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread
// g++ -g -O2 -DNDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread
#include <cstdio>
#include <iostream>
#include <osrng.h>
using CryptoPP::AutoSeededRandomPool;

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::exit;

#include <cryptlib.h>
using CryptoPP::Exception;

#include <hex.h>
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include <filters.h>
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;

#include <des.h>
using CryptoPP::DES_EDE2;

#include <modes.h>
using CryptoPP::CBC_Mode;

#include <secblock.h>
using CryptoPP::SecByteBlock;
#include <iostream>
#include <string>
#include <modes.h>
#include <aes.h>
#include <filters.h>
CryptoPP::SecByteBlock HexDecodeString(const char *hex)
CryptoPP::StringSource ss(hex, true, new CryptoPP::HexDecoder);
CryptoPP::SecByteBlock result((size_t)ss.MaxRetrievable());
ss.Get(result, result.size());
return result;

int main(int argc, char* argv[]) {

//HMODULE DLL = LoadLibrary(_T("cryptopp.dll"));
// Key and IV setup
//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
//bit). This key is secretly exchanged between two parties before communication
//begins. DEFAULT_KEYLENGTH= 16 bytes
std::string key = "0123456789abcdef";
std::string iv = "aaaaaaaaaaaaaaaa";
string plain = "CBC Mode Test";
string cipher, encoded, recovered;

std::string plaintext = "name macmilan age 24 ciy bonn country germany";
std::string ciphertext;
std::string decryptedtext;

std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;

CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, (byte *)iv.c_str() );

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
cout << "cipher text plain: " << ciphertext << endl;
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
cout << endl;
cout << endl;
std::cout <<"cipher text In HEX FORM:: ";
for( int i = 0; i < ciphertext.size(); i++ ) {

std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
cout << endl;
cout << endl;

// Pretty print
StringSource(ciphertext, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "cipher text In HEX FORM (Modified):: " << encoded << endl;
cout << endl;
cout << endl;
char *name2;
name2 = (char*) malloc(encoded.length() + 1); // don't forget to free!!!!
//s2 = Database_row_count; // I forget if the string class can implicitly be converted to char*
//s2[0] = '1';
strcpy(name2, encoded.c_str());

const char* hex_str = name2;

std::string result_string ;
unsigned int ch ;
for( ; std::sscanf( hex_str, "%2x", &ch ) == 1 ; hex_str += 2 )
result_string += ch ;
cout << "HEX FORM to cipher text :: " ;
std::cout << result_string << '\n' ;
cout << endl;
cout << endl;

CryptoPP::AES::Decryption aesDecryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte *)iv.c_str() );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( result_string.c_str() ), result_string.size() );
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;


return 0;

Step 7 – IF you the compiling your code in Debug Mode make sure in the Project-> properties->C/C++-> Code Generation ->Runtime Library the option ‘Multi-threaded Debug (/MTd)’is selected

IF you the compiling your code in Release Mode make sure in the Project-> properties->C/C++-> Code Generation ->Runtime Library the option ‘Multi-threaded DLL (/MD)’is selected

Now compile the program And Run it.
That’s it.



Compiling and Integrating Crypto++ into the Microsoft Visual C++ .c++ - How do I install Crypto++ in Visual Studio 2010 Windows 7.
How to build C++ cryptographic library, Crypto++ - NuLL
How to build Crypto++ dynamically on Microsoft Windows and Visual C++? ... is that your are linking against different versions of the run-time libraries....Visual C++ >>crypto++ compiler errors
Crypto++ Library 5.6.2 - a Free C++ Class Library of Cryptographic
crypto++ compiler error
encryption.cryptopp - Re: How to use Crypto++ with Visual C++ 6.0
Searches related to how to compile and run crypto++ in visual c++
visual c++ run program
visual studio run
visual basic run Compile cryptopp using Visual C++ 2010 Express.....c++ - How do I install Crypto++ in Visual Studio 2010 Windows 7
Crypto++ Faq-O-Matic: I compiled cryptest.exe successfully
encryption.cryptopp - Re: crypto++ in VisualC++ MFC 

IT Certification Category (English)640x480

Partner Sites

Top Online Courses From ProgrammingKnowledge

Python Course
Java Course
Bash Course
Linux Course
C Course
C++ Course
PHP Course
Android Course
C# Course
JavaFx Course
NodeJs Course
Jenkins Course
Scala Course
Bootstrap Course
MongoDB Course
QT C++ GUI Course