title: AES/CTR decryption stops unexpectedly using Crypto++?
body: <p>I am trying to AES encrypt and decrypt a byte array (<code>vector&lt;unsigned char&gt;</code>) with the <strong>Crypto++ library</strong>. Encryption works correctly.</p>
  
  <p>My function generates a random IV and puts it in front of the encrypted data. Decryption of course reads the IV. I modified the code from <a href="https://stackoverflow.com/a/12307096/9333301">this</a> answer, so it works with my vectors, and CTR/No Padding.</p>
  
  <p>The program crashes in the line <code>stfDecryptor.Put(...)</code>. First I thought the data or IV is read not correctly, but after checking, this is not the case. </p>
  
  <p>I hope I'm making a really obvious mistake here.</p>
  
  <p>Thanks guys :)</p>
  
  <p>Class call:</p>
  
  <pre><code>//The code is not clean, i want to get it working first and then clean it up. 
  auto key = Helper::RandomBytes(16); //returns "byte Array" with random Bytes
  auto data = Helper::UTF8String2ByteArray("HELLO WORLD!");
  auto encrypted = AESCrypt::encrypt(key, data);
  cout &lt;&lt; Helper::ByteArray2HexString(key);
  cout &lt;&lt; "\r\n";
  cout &lt;&lt; Helper::ByteArray2HexString(data);
  cout &lt;&lt; "\r\n";
  cout &lt;&lt; Helper::ByteArray2HexString(encrypted);
  cout &lt;&lt; "\r\n";    
  auto decrypted = AESCrypt::decrypt(key, encrypted);
  auto clear = Helper::ByteArray2UTF8String(decrypted);
  </code></pre>
  
  <p>Class: </p>
  
  <pre><code>vector&lt;unsigned char&gt; AESCrypt::encrypt(vector&lt;unsigned char&gt; key_, vector&lt;unsigned char&gt; data)
  {
      vector&lt;unsigned char&gt; iv_ = Helper::RandomBytes(16); //returns "byte Array" with random Bytes
  
      byte key[AES::DEFAULT_KEYLENGTH], iv[AES::BLOCKSIZE];
  
      for (size_t i = 0; i &lt; AES::DEFAULT_KEYLENGTH; i++)
          key[i] = key_[i];
      for (size_t i = 0; i &lt; iv_.size(); i++)
          iv[i] = iv_[i];
  
      AES::Encryption aesEncryption(key, AES::DEFAULT_KEYLENGTH);
      CTR_Mode_ExternalCipher::Encryption ctrEncryption(aesEncryption, iv);
  
      vector&lt;unsigned char&gt; encrypted;
      StreamTransformationFilter stfEncryptor(
          ctrEncryption,
          new VectorSink(encrypted),
          BlockPaddingSchemeDef::NO_PADDING);
  
      stfEncryptor.Put((const byte*)data.data(), data.size());
      stfEncryptor.MessageEnd();
  
      vector&lt;unsigned char&gt; output(encrypted.size() + 16);
  
      for (size_t i = 0; i &lt; 16; i++)
          output[i] = iv[i];
      for (size_t i = 0; i &lt; encrypted.size(); i++)
          output[16 + i] = encrypted[i];
  
      return output;
  }
  
  vector&lt;unsigned char&gt; AESCrypt::decrypt(vector&lt;unsigned char&gt; key_, vector&lt;unsigned char&gt; data_)
  {    
      byte key[AES::DEFAULT_KEYLENGTH], iv[AES::BLOCKSIZE];
      for (size_t i = 0; i &lt; AES::DEFAULT_KEYLENGTH; i++)
          key[i] = key_[i];
      for (size_t i = 0; i &lt; 16; i++)
          iv[i] = data_[i];
  
      vector&lt;unsigned char&gt; data(data_.size() - 16);
      for (size_t i = 0; i &lt; data.size(); i++)
      {
          data[i] = data_[16 + i];
      }
  
      AES::Decryption aesDecryption(key, AES::DEFAULT_KEYLENGTH);
      CTR_Mode_ExternalCipher::Decryption ctrDecryption(aesDecryption, iv);
  
      vector&lt;unsigned char&gt; decrypted;
      StreamTransformationFilter stfDecryptor(
          ctrDecryption,
          new VectorSink(decrypted),
          BlockPaddingSchemeDef::NO_PADDING);
  
      stfDecryptor.Put((const byte*)data.data(), data.size()); //this is where it crashes
      stfDecryptor.MessageEnd();
  
      return decrypted;
  
  }
  </code></pre>
  
  <p>Here is a picture off CallStack:</p>
  
  <p><a href="https://i.stack.imgur.com/Py5lv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Py5lv.png" alt="enter image description here"></a></p>
  
  <p>This is the line where it stopps inside the Crypto++ Libary: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>CRYPTOPP_ASSERT(m_cipher-&gt;IsForwardTransformation());
  </code></pre>