Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:02:17+00:00 2026-06-14T06:02:17+00:00

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

  • 0

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();
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:02:18+00:00Added an answer on June 14, 2026 at 6:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to play video from youtube using this code but I get this
Update edition: So, I'm trying to get this code to work without using scanf/fgets.
I am trying to get this to work I am using this code and
I'm trying to get this code to work, from here char *mem = (unsigned
Forgive me for this unbeautiful code, but I'm trying to get it to work
I'm trying to get this C++ code to input a series of numbers from
I'm trying to write this code using mostly arrays. I'm trying to get Correct
Im trying to get random number between -5 and 5. Im using this code:
I've been trying to get this code to work for hours! All I need
I'm trying to get work this code: $(#list).jqGrid({ url: 'employers.php', datatype: 'json', mtype: 'POST',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.