# AES Encryption Using Crypto++ .lib in Visual Studio C++

crypto.cpp

​

\#include "pch.h"

\#include

\#include "aes.h"

\#include

​

\#include "osrng.h"

using CryptoPP::AutoSeededRandomPool;

​

\#include

using std::cout;

using std::cerr;

using std::endl;

​

\#include

using std::string;

​

\#include

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 "aes.h"

using CryptoPP::AES;

​

\#include "ccm.h"

using CryptoPP::CBC\_Mode;

​

\#include "assert.h"

​

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

{

&#x20;AutoSeededRandomPool prng;

​

&#x20;byte key\[AES::DEFAULT\_KEYLENGTH];

&#x20;prng.GenerateBlock(key, sizeof(key));

​

&#x20;byte iv\[AES::BLOCKSIZE];

&#x20;prng.GenerateBlock(iv, sizeof(iv));

​

&#x20;string plain = "CBC Mode Test";

&#x20;string cipher, encoded, recovered;

​

​

&#x20;encoded.clear();

&#x20;StringSource(key, sizeof(key), true,

&#x20;new HexEncoder(

&#x20;new StringSink(encoded)

&#x20;)

&#x20;);

&#x20;cout << "key: " << encoded << endl;

​

&#x20;encoded.clear();

&#x20;StringSource(iv, sizeof(iv), true,

&#x20;new HexEncoder(

&#x20;new StringSink(encoded)

&#x20;)

&#x20;);

&#x20;cout << "iv: " << encoded << endl;

​

​

&#x20;try

&#x20;{

&#x20;cout << "plain text: " << plain << endl;

​

&#x20;CBC\_Mode< AES >::Encryption e;

&#x20;e.SetKeyWithIV(key, sizeof(key), iv);

​

&#x20;StringSource s(plain, true,

&#x20;new StreamTransformationFilter(e,

&#x20;new StringSink(cipher)

&#x20;)

&#x20;);

​

\#if 0

&#x20;StreamTransformationFilter filter(e);

&#x20;filter.Put((const byte\*)plain.data(), plain.size());

&#x20;filter.MessageEnd();

​

&#x20;const size\_t ret = filter.MaxRetrievable();

&#x20;cipher.resize(ret);

&#x20;filter.Get((byte\*)cipher.data(), cipher.size());

\#endif

&#x20;}

&#x20;catch (const CryptoPP::Exception& e)

&#x20;{

&#x20;cerr << e.what() << endl;

&#x20;exit(1);

&#x20;}

​

​

&#x20;encoded.clear();

&#x20;StringSource(cipher, true,

&#x20;new HexEncoder(

&#x20;new StringSink(encoded)

&#x20;)

&#x20;);

&#x20;cout << "cipher text: " << encoded << endl;

​

​

&#x20;try

&#x20;{

&#x20;CBC\_Mode< AES >::Decryption d;

&#x20;d.SetKeyWithIV(key, sizeof(key), iv);

​

&#x20;StringSource s(cipher, true,

&#x20;new StreamTransformationFilter(d,

&#x20;new StringSink(recovered)

&#x20;)

&#x20;);

​

\#if 0

&#x20;StreamTransformationFilter filter(d);

&#x20;filter.Put((const byte\*)cipher.data(), cipher.size());

&#x20;filter.MessageEnd();

​

&#x20;const size\_t ret = filter.MaxRetrievable();

&#x20;recovered.resize(ret);

&#x20;filter.Get((byte\*)recovered.data(), recovered.size());

\#endif

​

&#x20;cout << "recovered text: " << recovered << endl;

&#x20;}

&#x20;catch (const CryptoPP::Exception& e)

&#x20;{

&#x20;cerr << e.what() << endl;

&#x20;exit(1);

&#x20;}

​

​

&#x20;return 0;

}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pwc-3.gitbook.io/pwc/ji-shu/untitled-1/miscellaneous-reversing-forensics/aes-encryption-using-crypto++-.lib-in-visual-studio-c++.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
