仅在 Crypto++ 中进行 AES 解密

AES Decryption only in Crypto++

我有一个基于 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>
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))));