Click here to Skip to main content
15,918,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#pragma comment (lib,"cryptlib.lib")
#include <cstdio>
#include <cderr.h>
#include <osrng.h>
using CryptoPP::AutoSeededRandomPool;

#include <string>
#include <cstdlib>
#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>
using namespace std;

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

	string key = "0123456789abcdef";
	string iv = "aaaaaaaaaaaaaaaa";
	string plain = "CBC Mode Test";
	string cipher, encoded, recovered;

	string plaintext = "mr khakpour plain text example.";
	string ciphertext;
	string decryptedtext;

	cout << "Plain Text (" << plaintext.size() << " bytes)" << endl;
	cout << plaintext;
	cout << endl << 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;
	cout << "Cipher Text (" << ciphertext.size() << " bytes)" << endl;
	cout << endl;
	cout << endl;
	cout << "cipher text In HEX FORM:: ";
	for (int i = 0; i < ciphertext.size(); i++) {

		cout << "0x" << 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: " << encoded << endl;
	cout << endl;
	cout << endl;
	char *name2;
	name2 = (char*)malloc(encoded.length() + 1); //  free!!!!
	strcpy(name2, encoded.c_str());

	const char* hex_str = name2;

	string result_string;
	unsigned int ch;
	for (; sscanf(hex_str, "%2x", &ch) == 1; hex_str += 2)
		result_string += ch;
	cout << "HEX FORM to cipher text :: ";
	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());
	cout << "Decrypted Text: " << endl;
	cout << decryptedtext;
	cout << endl << endl;


	return 0;

i am compile this program on dev c++ compiler but i face error
plese help me to run this program on Dev C++
Updated 8-Feb-15 23:39pm
Richard MacCutchan 9-Feb-15 5:40am    
We cannot gues what error you face, you have to tell us what it is (exact message), and where it occurs (compile time, link time, execution time?).
saeedbahal 9-Feb-15 6:07am    
ok , this error is one of the errors that occur in compile:
[Linker error] undefined reference to `CryptoPP::CipherModeFinalTemplate_ExternalCipher<cryptopp::cbc_encryption>::CipherModeFinalTemplate_ExternalCipher(CryptoPP::BlockCipher&, unsigned char const*, int)'

other error similar this error

The code snippet is for Visual C++ but you are using Dev C++.

The first line
#pragma comment (lib,"cryptlib.lib")

is Microsoft specific and links the application with the specified library. You must use the appropiate steps for your development environment to link your application with that library.

However, you must ensure to have a library version that is compatible with your development environment. If necessary you must build the library using your Dev C++.
Share this answer
using dev cpp 5.6.3 compiler and add libcryptopp.a file to project and delete first line in code
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900