#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 "..\cryptopp\osrng.h"
#pragma warning (disable : 4996)
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 "..\cryptopp\cryptlib.h"
using CryptoPP::Exception;
#include "..\cryptopp\hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include "..\cryptopp\filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;
#include "..\cryptopp\des.h"
using CryptoPP::DES_EDE2;
#include "..\cryptopp\modes.h"
using CryptoPP::CBC_Mode;
#include "..\cryptopp\secblock.h"
using CryptoPP::SecByteBlock;
#include <iostream>
#include <string>
#include "..\cryptopp\modes.h"
#include "..\cryptopp\aes.h"
#include "..\cryptopp\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);
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 (size_t i = 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,
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_s(name2, encoded.length() + 1, encoded.c_str());
const char* hex_str = name2;
std::string result_string;
unsigned int ch;
for (; sscanf_s(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());
stfDecryptor.MessageEnd();
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
system("pause");
return 0;
}
Ho un problema sulla 120 riga (result_string += ch;), con visual studio 2015:
warning C4242: 'argument': conversion from 'unsigned int' to 'char', possible loss of data.
e anche fatal error LNK1104: cannot open file 'D:\Download\Scuola\C++\Visual Studio\Prova 2\cryptopp\Win32\Output\Debug.obj' ma quest'ultimo credo sia dovuto alla mancanza di un file e dovrei risolverlo.
Grazie mille.