如何解决我们如何使用 Crypto++ 执行 AES 文件加密?
我是 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,iv);
StringSource s(cipher,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
解决方法
从简单开始。加密很复杂,需要包含很多额外的位。对于第一次试用,您需要一个密钥、一个模式(CBC 相当简单)和一个 IV(初始化向量)。设置它,测试它并让它工作。然后你可以添加诸如密钥派生之类的东西,并尝试更复杂的模式,比如 GCM。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。