【发布时间】:2017-03-02 20:27:17
【问题描述】:
我正在尝试测试 AES 加密的性能。但是每当我运行代码时,它都会给出不同的结果。为什么? 这是使用 Crypto++ 的 C++ 代码:
int main(int argc, char* argv[]){
AutoSeededRandomPool prng;
byte key[AES::DEFAULT_KEYLENGTH];
prng.GenerateBlock(key, sizeof(key));
byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, sizeof(key), iv);
CBC_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, sizeof(key), iv);
时间测试在这里:
clock_t startTime, finishTime;
std::string plain = "AES CBC Test";
std::string cipher, encoded, recovered;
startTime = clock();
try
{
// The StreamTransformationFilter removes
// padding as required.
StringSource s(plain, true,
new StreamTransformationFilter(e,
new StringSink(cipher)
) // StreamTransformationFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}
// save current time just after finishing the encryption loop
finishTime = clock();
我的测试结果在这里:
enter code heredouble executionTimeInSec = double( finishTime - startTime ) / CLOCK_TICKS_PER_SECOND;
std::cout << "Encryption loop execution time: " << executionTimeInSec * 1000.0 << " microseconds." << std::endl;
std::cout << "Plain text size: " << plain.size() << " bytes." << std::endl;
double data_rate_MiBps = ((double)plain.size() / 1048576) / ((double)executionTimeInSec) ;
std::cout << "Encryption/decryption loop execution time MB/S: " << data_rate_MiBps << " MB/S." << std::endl;
return 0;}
定时未优化的调试构建。 编译结果1:
加密循环执行时间:0.041 微秒。
编译结果2:
加密循环执行时间:0.057 微秒。
【问题讨论】:
-
如果您的代码实际编译,这将有助于您获得答案。
-
您还应该发布您是在计时优化、发布构建还是未优化的“调试”构建。如果是后者,那么这些计时就没有意义了。
-
您的代码不完整;特别是,它似乎至少缺少一个
#include。请edit你的代码,所以这是你的问题的minimal reproducible example,然后我们可以尝试重现并解决它。您还应该阅读How to Ask。 -
另请参阅 Crypto++ wiki 上的 Benchmarks 和 Stack Overflow 上的 Calculate time encryption of AES/CCM in Visual Studio 2017。
标签: c++ cryptography aes benchmarking crypto++