title: StreamTransformationFilter: ciphertext length is not a multiple of block size?
body: <p>I am trying to encrypt and decrypt a plain text using aes algorithm present in <a href="https://github.com/weidai11/cryptopp" rel="nofollow noreferrer">crypto++</a></p>
  
  <p>Here is my encryption method</p>
  
  <pre><code>/*
   * Encrypt the given text
   */
  
  template&lt;typename T&gt;
  T encryptText(T plainText) {
      /* Key and IV setup
       * AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-   
       * bit). This key is secretly exchanged between two parties before communication   
       * begins. DEFAULT_KEYLENGTH= 16 bytes
       */
      byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
      memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
      memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
      T cipherText;
      cout &lt;&lt; "\n\nPlain Text size is (" &lt;&lt; plainText.size() &lt;&lt; " bytes)" &lt;&lt; "\n\n";
      cout &lt;&lt; plainText &lt;&lt; "\n\n";
      /*
       * Create Cipher Text
       */
      CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
      CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
      CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ) );
      stfEncryptor.Put( reinterpret_cast&lt;const unsigned char*&gt;( plainText.c_str() ), plainText.length() + 1 );
      stfEncryptor.MessageEnd();
      cout &lt;&lt; "\n\nCipher Text size is (" &lt;&lt; cipherText.size() &lt;&lt; " bytes)" &lt;&lt; std::endl;
      for( int i = 0; i &lt; cipherText.size(); i++ ) {
          cout &lt;&lt; "0x" &lt;&lt; std::hex &lt;&lt; (0xFF &amp; static_cast&lt;byte&gt;(cipherText[i])) &lt;&lt; " ";
      }
      return cipherText;
  }
  </code></pre>
  
  <p>Here is my decryption method</p>
  
  <pre><code>/*
   * Decrypt the given text
   */
  
  template&lt;typename T&gt;
  T decryptText(T encryptedText) {
      /* Key and IV setup
       * AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-   
       * bit). This key is secretly exchanged between two parties before communication   
       * begins. DEFAULT_KEYLENGTH= 16 bytes
       */
      byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
      memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
      memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
      CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
      CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
      T decryptedText;
      CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ) );
      stfDecryptor.Put( reinterpret_cast&lt;const unsigned char*&gt;( encryptedText.c_str() ), encryptedText.size() );
      stfDecryptor.MessageEnd();
      std::cout &lt;&lt; "\n\nDecrypted Text is " &lt;&lt; "\n\n";
      std::cout &lt;&lt; "\n\n" &lt;&lt; decryptedText &lt;&lt; "\n\n";
      return decryptedText;
  }
  </code></pre>
  
  <p><code>encryptText</code> function execute normally and i save the output of it in mysql database. While calling the <code>decryptText</code> function an error occurs saying </p>
  
  <pre><code>An uncaught exception occurred: StreamTransformationFilter: ciphertext length is not a multiple of block size
  </code></pre>
  
  <p>And i understand that this error is because of encrypted text may contain some null(0) value too. How decrypt the actual text. </p>
  
  <p><strong>Edit :</strong></p>
  
  <p>As i asked to show my sql queries in comment section. </p>
  
  <p>Here is my sql query to fetch the saved encrypted data in mysql database.</p>
  
  <pre><code>template&lt;typename T1, typename T2&gt;
  void getUserSms(T1 userId,vector&lt;T1&gt;&amp; smsIds,vector&lt;T2&gt;&amp; text){
      try{
          mysql::connection db_cs(config_cs_db());
          auto sms = db_cs.run(select(all_of(us)).from(us).where(us.usersUserId == userId));
          while(!sms.empty()){
              const auto&amp; row = sms.front();
              auto smsId = row.id.value();
              smsIds.push_back(smsId);
              auto encryptedText = row.text.value();
              auto plainText = decryptText(encryptedText);
              text.push_back(plainText);
              sms.pop_front();
          }
          return;
      }
      catch (const sqlpp::exception&amp; e) {
          std::cerr &lt;&lt; "Could not get user sms due to some reason ...!!\n";
          std:cerr &lt;&lt; e.what() &lt;&lt; "\n";
          return;
      }
  }
  </code></pre>
  
  <p>I am using <a href="https://github.com/rbock/sqlpp11" rel="nofollow noreferrer">sqlpp11</a> for sql queries. And the table description is</p>
  
  <pre><code>CREATE TABLE `user_sms`(
  `id` BIGINT(50) NOT NULL AUTO_INCREMENT,
  `sender_number` VARCHAR(20),
  `text` VARCHAR(2000),
  `incoming_time` TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `users_user_id` BIGINT(30),
  PRIMARY KEY(`id`),
  FOREIGN KEY(`users_user_id`) REFERENCES `users`(`user_id`)
  )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>