Monday, April 15, 2013

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

11:59 AM


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



Step 1- Download the latest Crypto++ library from the link http://www.cryptopp.com/#download. 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>
usingCryptoPP::AutoSeededRandomPool;

#include<iostream>
usingstd::cout;
usingstd::cerr;
usingstd::endl;

#include<string>
usingstd::string;

#include<cstdlib>
usingstd::exit;

#include<cryptlib.h>
usingCryptoPP::Exception;

#include<hex.h>
usingCryptoPP::HexEncoder;
usingCryptoPP::HexDecoder;

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

#include<des.h>
usingCryptoPP::DES_EDE2;

#include<modes.h>
usingCryptoPP::CBC_Mode;

#include<secblock.h>
usingCryptoPP::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;
}*/


intmain(intargc, 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::stringkey = "0123456789abcdef";
std::stringiv = "aaaaaaaaaaaaaaaa";
stringplain = "CBC Mode Test";
stringcipher, encoded, recovered;


std::stringplaintext = "name macmilan age 24 ciy bonn country germany";
std::stringciphertext;
std::stringdecryptedtext;

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

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

CryptoPP::StreamTransformationFilterstfEncryptor(cbcEncryption, newCryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<constunsignedchar*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
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( inti = 0; i < ciphertext.size(); i++ ) {

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

// Pretty print
encoded.clear();
StringSource(ciphertext, true,
newHexEncoder(
newStringSink(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());

constchar* hex_str = name2;

std::stringresult_string ;
unsignedintch ;
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::DecryptionaesDecryption((byte*)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::DecryptioncbcDecryption( aesDecryption, (byte*)iv.c_str() );

CryptoPP::StreamTransformationFilterstfDecryptor(cbcDecryption, newCryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<constunsignedchar*>( result_string.c_str() ), result_string.size() );
stfDecryptor.MessageEnd();
std::cout << "Decrypted Text: "<< std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

system("pause");

return0;
}


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 

Written by

We are one of the initiators of the development of information technology in understanding the need for a solution that is familiar and close to us.

0 comments:

Post a Comment

 

© 2013 Klick Dev. All rights resevered.

Back To Top