c++ - AES 和 key 长度错误

标签 c++ key aes crypto++

我正在尝试让这段代码工作。来自Cryptopp AES

Demonstrates encryption and decryption using AES in CTR

唯一的区别是,我创建了函数 encryptAESdecryptAES 并插入了代码。 它无需创建这些功能即可工作。但现在我收到以下错误:AES/CTR 4 不是有效的 key 长度,但 key 长度为 16 位。

string encryptAES(const byte key[], const string& plain, const byte iv[])
{
try
{
    string cipher;

    CTR_Mode< AES >::Encryption e;
    e.SetKeyWithIV(key, sizeof(key), iv);

    // The StreamTransformationFilter adds padding
    //  as required. ECB and CBC Mode must be padded
    //  to the block size of the cipher.
    StringSource(plain, true, 
        new StreamTransformationFilter(e,
            new StringSink(cipher)
        ) // StreamTransformationFilter      
    ); // StringSource
    return cipher;
}
catch(const CryptoPP::Exception& e)
{
    cerr << e.what() << endl;
    return "";
}
}

string decryptAES(const byte key[], const string& cipher, const byte iv[])
{
try
{
    string recovered;

    CTR_Mode< AES >::Decryption d;
    d.SetKeyWithIV(key, sizeof(key), iv);

    // The StreamTransformationFilter removes
    //  padding as required.
    StringSource s(cipher, true, 
        new StreamTransformationFilter(d,
            new StringSink(recovered)
        ) // StreamTransformationFilter
    ); // StringSource
    return recovered;
}
catch(const CryptoPP::Exception& e)
{
    cerr << e.what() << endl;
    return "";
}
}

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));

string plain = "CTR Mode Test";
string encoded, cipher, recovered;

/*********************************\
\*********************************/

// Pretty print key
encoded.clear();
StringSource(key, sizeof(key), true,
    new HexEncoder(
        new StringSink(encoded)
    ) // HexEncoder
); // StringSource
cout << "key: " << encoded << endl;

// Pretty print iv
encoded.clear();
StringSource(iv, sizeof(iv), true,
    new HexEncoder(
        new StringSink(encoded)
    ) // HexEncoder
); // StringSource
cout << "iv: " << encoded << endl;

/*********************************\
\*********************************/

cout << "plain text: " << plain << endl;
cipher = encryptAES(key, plain, iv);

/*********************************\
\*********************************/

// Pretty print
encoded.clear();
StringSource(cipher, true,
    new HexEncoder(
        new StringSink(encoded)
    ) // HexEncoder
); // StringSource
cout << "cipher text: " << encoded << endl;

/*********************************\
\*********************************/

recovered = decryptAES(key, cipher, iv);
cout << "recovered text: " << recovered << endl;

cin.sync();
cin.get();
} 

最佳答案

您的函数正在使用 const byte key[]参数,它本质上被视为一个指针。因此 sizeof(key) 是您平台上指针的大小。

string encryptAES(const byte key[], const string& plain, const byte iv[])

// sizeof(key) is the size of a pointer
e.SetKeyWithIV(key, sizeof(key), iv);

您可以使用 std::vector<>作为选项,或传递 key_len,例如:

string encryptAES(const byte key[], size_t key_len, const string& plain, const byte iv[])

// using key_len for the length of the key
e.SetKeyWithIV(key, key_len, iv);

我希望这是有道理的,因为同样的错误出现在几个地方。

关于c++ - AES 和 key 长度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335688/

相关文章:

javascript - 你如何在 JavaScript 中编写来自 JSON 的空键?

json - 如何使用 jq 将多个输出对象组合成一个对象

ios - AES 加密和解密

c++ - 为什么共享指针不删除内存?

c++ - 安全地取消 boost asio 截止时间计时器

C#如何读取节点xml中的单个项目

java - 在 Java 中为 AES 生成随机 IV

struct - 在 golang 结构中转换字符串

c++ - 删除模板类型

c++ - std::vector 指针