[英]Using std::string for key with AES encryption in Crypto++
[英]How can we perform AES file encryption using Crypto++?
我是 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;
}
我也阅读了文件接收器和文件源文档,但实际上我无法在上面给出的代码中应用它。 任何帮助,将不胜感激。 https://www.cryptopp.com/wiki/Advanced_Encryption_Standard
从简单开始。 加密很复杂,需要包含许多额外的位。 对于第一次试验,您需要一个密钥、一个模式(CBC 相当简单)和一个 IV(初始化向量)。 设置它,测试它并让它工作。 然后您可以添加诸如密钥派生之类的东西并尝试更复杂的模式,例如 GCM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.