Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #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 "aes.h"
- using CryptoPP::AES;
- #include "modes.h"
- using CryptoPP::ECB_Mode;
- int main()
- {
- AutoSeededRandomPool prng;
- byte key[16];
- prng.GenerateBlock(key, sizeof(key));
- string plain = "mypassword";
- string cipher, encoded, recovered;
- /*********************************\
- \*********************************/
- encoded.clear();
- StringSource(key, sizeof(key), true,
- new HexEncoder(
- new StringSink(encoded)
- )
- );
- cout << "key: " << encoded << endl;
- /*********************************\
- \*********************************/
- try
- {
- cout << "plain text: " << plain << endl;
- ECB_Mode< AES >::Encryption e;
- e.SetKey(key, sizeof(key));
- StringSource(plain, true,
- new StreamTransformationFilter(e,
- new StringSink(cipher)
- )
- );
- }
- catch (const CryptoPP::Exception& e)
- {
- cerr << e.what() << endl;
- system("pause");
- exit(1);
- }
- /*********************************\
- \*********************************/
- // Pretty print
- encoded.clear();
- StringSource(cipher, true,
- new HexEncoder(
- new StringSink(encoded)
- )
- );
- cout << "cipher text: " << encoded << endl;
- /*********************************\
- \*********************************/
- try
- {
- ECB_Mode< AES >::Decryption d;
- d.SetKey(key, sizeof(key));
- StringSource s(cipher, true,
- new StreamTransformationFilter(d,
- new StringSink(recovered)
- )
- );
- cout << "recovered text: " << recovered << endl;
- }
- catch (const CryptoPP::Exception& e)
- {
- cerr << e.what() << endl;
- system("pause");
- exit(1);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement