我有一个基于crypto ++示例的方法(但使用硬编码密钥和iv),其中我加密和解密一个字符串,这很好。现在,当我注释掉加密代码并将密文设置为我从加密部分收到的输出时,我只得到废话而不是我开始使用的字符串。这是我的代码(加密部分已注释掉):
byte iv[CryptoPP::AES::BLOCKSIZE];
byte key[CryptoPP::AES::MAX_KEYLENGTH];
std::string strKey = "F5C3DD6EA4F2AE1713F4B9B21FD0011CCECB5CA8858F313B5EA3E85CD1F6E70E";
std::string striv = "DEA43ED679566B66FD2B5126149F34A";
int x = 0;
for (unsigned int i = 0; i < strKey.length(); i += 2) {
std::string bytestring = strKey.substr(i, 2);
key[x] = (char)strtol(bytestring.c_str(), NULL, 16);
x++;
}
int y = 0;
for (unsigned int i = 0; i < striv.length(); i += 2) {
std::string bytestring = striv.substr(i, 2);
iv[y] = (char)strtol(bytestring.c_str(), NULL, 16);
y++;
}
std::string plain = "test mode";
std::string cipher, encoded, recovered;
cipher = "F190D36A0FEEF07C5B";
/*
//print key
encoded.clear();
CryptoPP::StringSource(key, sizeof(key), true,
new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
std::cout << "key: " << encoded << std::endl;
//print iv
encoded.clear();
CryptoPP::StringSource(iv, sizeof(iv), true,
new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
std::cout << "iv: " << encoded << std::endl;
try
{
std::cout << "plain text: " << plain << std::endl;
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption en;
en.SetKeyWithIV(key, sizeof(key), iv);
CryptoPP::StringSource(plain, true, new CryptoPP::StreamTransformationFilter(en, new CryptoPP::StringSink(cipher)));
std::cout << "cipher: " << cipher;
}
catch (const std::exception& e)
{
std::cout << "CryptInit Exception: " << e.what() << std::endl;
}
encoded.clear();
CryptoPP::StringSource(cipher, true,
new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
std::cout << "cipher text: " << encoded << std::endl;
//*/
try
{
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption de;
de.SetKeyWithIV(key, sizeof(key), iv);
CryptoPP::StringSource s(cipher, true,
new CryptoPP::StreamTransformationFilter(de, new CryptoPP::StringSink(recovered)));
std::cout << "recovered text: " << recovered << std::endl;
}
catch (const std::exception& e)
{
std::cout << "CryptInit Exception: " << e.what() << std::endl;
}
这是我收到的输出:├─Ö.k░®Y♫μμâ├v
出于复制目的,这里是必需的包括:
#include <cryptopp\aes.h>
#include <cryptopp\filters.h>
#include <cryptopp\modes.h>
#include <cryptopp\hex.h>
#include <cryptopp\cryptlib.h>
答案 0 :(得分:1)
cipher = "F190D36A0FEEF07C5B";
和
CryptoPP::StringSource s(cipher, true,
new CryptoPP::StreamTransformationFilter(de, new CryptoPP::StringSink(recovered)));
看起来您正在尝试解密十六进制编码的字符串。您可能需要通过HexDecoder
运行密文。也许是这样的:
StringSource s(cipher, true,
new HexDecoder(
new StreamTransformationFilter(de, new StringSink(recovered))));