Вопрос

I am trying to get this code to work. It is from Cryptopp AES

Demonstrates encryption and decryption using AES in CTR

The only difference is, that I created the function encryptAES and decryptAES and inserted the code. It works without creating these functions. But now I got the following error: AES/CTR 4 is not a valid key length but the key is 16 bit long.

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();
} 
Это было полезно?

Решение

Your functions are taking a const byte key[] parameter, which is essentially treated as a pointer. thus sizeof(key) is the size of a pointer on your platform.

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

You can use a std::vector<> as an option, or pass the key_len, such as:

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

I hope that makes sense, because the same error is in a couple places.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top