AES和密钥长度错误。

3

我试图让这段代码工作。它来自Cryptopp AES

演示了使用CTR的AES进行加密和解密的过程。

唯一的区别是,我创建了函数encryptAESdecryptAES并插入了代码。虽然不创建这些函数也可以正常工作,但现在我遇到了以下错误:AES/CTR 4不是有效的密钥长度,但密钥为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();
} 

1
你的函数是根据退化指针的大小计算密钥大小的。即,sizeof(key)!=密钥数组中字节的大小。你需要一个key_len附加参数来同时调用这两个函数。 - WhozCraig
1个回答

2

您的函数接受一个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++非常新手。;) - nt2005

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接