我要求使用给定密钥在CBC模式下使用AES算法解密输入文件。我尝试使用在线工具AES – Symmetric Ciphers Online在CBC模式下使用AES算法加密样本文件。使用的密钥是" 208512335119421110996C7761792045" 。
我能够使用此工具加密文件并将加密文件解密回原始内容。我尝试使用带有以下代码的cryptopp库来做同样的事情,但它没有提供原始文件内容。
请指出我在这里做了什么错。
#include <iostream>
#include <fstream>
#include <exception>
#include <sstream>
#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
#include "cryptopp/filters.h"
#include "cryptopp/cryptlib.h"
using CryptoPP::Exception;
#include "cryptopp/hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include "cryptopp/filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;
#include "cryptopp/aes.h"
using CryptoPP::AES;
#include "cryptopp/ccm.h"
using CryptoPP::CBC_Mode;
using namespace std;
std::string getFstr(std::string fname)
{
std::ifstream ifs(fname.c_str());
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
return content;
}
int main(int argc, char* argv[])
{
try
{
std::string key_string = "208512335119421110996C7761792045";
std::string enctext;
byte no[] = {0x2,0x0,0x8,0x5,0x1,0x2,0x3,0x3,0x5,0x1,0x1,0x9,0x4,0x2,0x1,0x1,0x1,0x0,0x9,0x9,0x6,0xC,0x7,0x7,0x6,0x1,0x7,0x9,0x2,0x0,0x4,0x5};
byte noiv[32];
std::string out;
CryptoPP::AES::Encryption aesEncryption(no, 32);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, noiv );
std::string fileName("in.txt");
enctext = getFstr(fileName);
CryptoPP::AES::Decryption aesDecryption(no, 32);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, noiv );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( out ),CryptoPP::StreamTransformationFilter::NO_PADDING);
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( enctext.c_str() ), enctext.size() );
stfDecryptor.MessageEnd();
ofstream o("out.txt", ios::out|ios::binary);
if ( o.good() )
{
o.write(out.c_str(),out.size());
o.close();
}
return 0;
}
catch ( CryptoPP::Exception& e)
{
std::cout << std::endl << e.what() << std::endl;
}
}
提前致谢