【发布时间】:2021-07-30 12:50:27
【问题描述】:
我是 Crypto++ 的新手。 我已阅读 Crypto++ 网站上有关 AES 加密的文档,我想使用它执行 AES 文件加密。 他们的示例代码如下所示:
#include "cryptlib.h"
#include "rijndael.h"
#include "modes.h"
#include "files.h"
#include "osrng.h"
#include "hex.h"
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
using namespace CryptoPP;
AutoSeededRandomPool prng;
HexEncoder encoder(new FileSink(std::cout));
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
SecByteBlock iv(AES::BLOCKSIZE);
prng.GenerateBlock(key, key.size());
prng.GenerateBlock(iv, iv.size());
std::string plain = "CBC Mode Test";
std::string cipher, recovered;
std::cout << "plain text: " << plain << std::endl;
/*********************************\
\*********************************/
try
{
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, key.size(), iv);
StringSource s(plain, true,
new StreamTransformationFilter(e,
new StringSink(cipher)
) // StreamTransformationFilter
); // StringSource
}
catch(const Exception& e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
/*********************************\
\*********************************/
std::cout << "key: ";
encoder.Put(key, key.size());
encoder.MessageEnd();
std::cout << std::endl;
std::cout << "iv: ";
encoder.Put(iv, iv.size());
encoder.MessageEnd();
std::cout << std::endl;
std::cout << "cipher text: ";
encoder.Put((const byte*)&cipher[0], cipher.size());
encoder.MessageEnd();
std::cout << std::endl;
/*********************************\
\*********************************/
try
{
CBC_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, key.size(), iv);
StringSource s(cipher, true,
new StreamTransformationFilter(d,
new StringSink(recovered)
) // StreamTransformationFilter
); // StringSource
std::cout << "recovered text: " << recovered << std::endl;
}
catch(const Exception& e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
return 0;
}
我也阅读了 filesink 和 filesource 文档,但我实际上无法在上面给出的代码中应用它。 任何帮助,将不胜感激。 https://www.cryptopp.com/wiki/Advanced_Encryption_Standard
【问题讨论】:
标签: c++ encryption aes crypto++