0

I am trying to pass 2 byte arrays to a file encryption function I wrote. However, when I do both arrays are truncated to size 8 (from size 16) thus causing the function to not work properly. I am not sure what I am doing wrong here since the function parameters are simply pointing to the byte arrays.

   CryptoPP::AutoSeededRandomPool prng;
   //Key generation
   byte key[AES::DEFAULT_KEYLENGTH];
   prng.GenerateBlock(key, sizeof(key));
   //IV generation
   byte iv[AES::BLOCKSIZE];
   prng.GenerateBlock(iv, sizeof(iv));



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

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

   //See function below
   encrypt_file(inFile, outFile, key, iv, err); 

   inFile.close();
   outFile.close();

Once in this function the bytes arrays are truncated for some reason

Encrypt_file

    bool encrypt_file(std::ifstream& inFile,
       std::ofstream& outFile,
       const byte* key, const byte* iv,
       std::string& errMsg)
    {
       std::string encoded;
       //print key
       encoded.clear();
       StringSource(key, sizeof(key), true, new HexEncoder(new StringSink(encoded)));
       cout << "key: " << encoded << endl;
       cout << "Size of key: " << sizeof(key) << endl;

       //print iv
       encoded.clear();
       StringSource(iv, sizeof(iv), true, new HexEncoder(new StringSink(encoded)));
       cout << "iv: " << encoded << endl;
       cout << "Size of iv: " << sizeof(iv) << endl;
       try {
          CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption e;
          e.SetKeyWithIV(key, sizeof(key), iv);
          CryptoPP::FileSource(inFile, true, new CryptoPP::StreamTransformationFilter(e, new CryptoPP::FileSink(outFile)));
          inFile.close();
          outFile.close();
       }
       catch (CryptoPP::Exception& e) {
          errMsg = e.GetWhat();
          return false;
       }
       return true;
    }

Output:

key: 6574D7BDFD0DD3BC59CD3846D4A196A8
Size of key: 16
iv: 1B4ED692F91A32246B41F63F6B8C6EAA
Size of iv: 16
key: 6574D7BDFD0DD3BC
Size of key: 8
iv: 1B4ED692F91A3224
Size of iv: 8
jww
  • 83,594
  • 69
  • 338
  • 732
  • 4
    `sizeof(key)` is the size of the pointer, not the size of the data it points to. The latter, you'd have to pass as an extra parameter (just like you do with `StringSource`) – Igor Tandetnik Mar 02 '17 at 04:35
  • 1
    Also see [What is array decaying?](http://stackoverflow.com/q/1461432/608639), [Why does a C-Array have a wrong sizeof() value when it's passed to a function?](http://stackoverflow.com/q/2950332/608639), [Sizeof an array in the C programming language?](http://stackoverflow.com/q/1975128/608639), etc. – jww Mar 02 '17 at 06:05
  • awesome links! thank you! –  Mar 02 '17 at 13:58

0 Answers0