cryptoPP AES,奇怪的加密输出
cryptoPP AES, weird encryption output
我是新的并测试出cryptoPP,我尝试使用cryptoPP一次加密单个字符,我注意到一些字符不能正确加密并表现出奇怪的行为,如多于或少于16个字符,或奇怪的安排。
我使用的代码是:
std::string data;
std::cin >> data;
std::string plaintext = data;
std::string ciphertext;
byte key[16 ] = {1,2,3,5,6,7,8,9,0,1,2,3,4,5,6} ;
byte iv[ 16 ]={1,2,3,5,6,7,8,9,0,1,2,3,4,5,6};
memset( key, 0, sizeof(key));
memset( iv, 0, sizeof(iv) );
CryptoPP::AES::Encryption aesEncryption(key, 16);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length());
stfEncryptor.MessageEnd();
std::cout << ciphertext << std::endl;
std::ofstream file("d:\log.txt", std::ofstream::out | std::ofstream::app);
file.write(ciphertext.c_str(), ciphertext.size());
file.close();
system("PAUSE");
解密代码如下:
byte key[16 ] = {1,2,3,5,6,7,8,9,0,1,2,3,4,5,6} ;
byte iv[ 16 ]={1,2,3,5,6,7,8,9,0,1,2,3,4,5,6};
memset( key, 0, 16 );
memset( iv, 0, 16 );
std::string decryptedtext;
std::string test = get_file_contents("d:\log.txt");
for (int i = 0; i < test.length() ; i += 16) {
std::string block = test.substr(i, i + 16);
test.replace(i, i + 16, block);
CryptoPP::AES::Decryption aesDecryption(key, 16);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( block.c_str() ), 16 );
stfDecryptor.MessageEnd();
}
std::cout << decryptedtext << std::endl;
std::ofstream fout("d:\aesencrypt.txt");
fout << decryptedtext;
如果我不输入某些字符,它会工作。当我输入字符"c"或"r"时发生问题,它变得奇怪,当我解密时我无法解析16个字符。解密将失败,因为加密文本不对齐或多于或少于16个字符。
我完全运行你的代码没有写/读/从文件,它的工作。(它也适用于"c"answers"r")。我把这两个片段合二为一,去掉文件,分配给test = ciphertext;
。也许问题出在读/写文件上…
加密一个或多个字符并不重要:如果输入不是块大小的倍数,CBC模式会添加填充。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- 异或字符串加密/解密输出错误
- C++Openssl加密例程在每次运行时生成不同的输出
- 将Botan加密结果输出到QDomDocument,反之亦然
- 为什么我的OpenSSL C++代码会创建二进制加密输出
- cryptoPP AES,奇怪的加密输出