title: AES with padding pkcs7 c++ code
body: <p>I need an example of string encryption (in C++ -> I'm working on linux-Ubuntu) with aes-cbc256 and a padding: PKCS7
  Please help.</p>
  
  <hr>
  
  <p>For the following code how can I set the IV to 0 and set the key value to a string value? I would also like to add the pkcs7 padding. I'm using the crypto++ lib (in Linux)</p>
  
  <pre><code>// Driver.cpp   
  //      
  
  #include "stdafx.h"    
  #include "cryptopp/dll.h"    
  #include "cryptopp/default.h"    
  #include "crypto++/osrng.h"    
  using CryptoPP::AutoSeededRandomPool;    
  
  #include &lt;iostream&gt;    
  using std::cout;    
  using std::cerr;       
  
  #include &lt;string&gt;    
  using std::string;       
  
  #include "crypto++/cryptlib.h"    
  using CryptoPP::Exception;        
  
  #include "crypto++/hex.h"    
  using CryptoPP::HexEncoder;    
  using CryptoPP::HexDecoder;        
  
  #include "crypto++/filters.h"    
  using CryptoPP::StringSink;    
  using CryptoPP::StringSource;    
  using CryptoPP::StreamTransformationFilter;        
  
  #include "crypto++/aes.h"    
  using CryptoPP::AES;       
  
  #include "crypto++/ccm.h"    
  using CryptoPP::CBC_Mode;       
  
  #include "assert.h"        
  
  int main(int argc, char* argv[])    
  {    
      AutoSeededRandomPool prng;        
  
      byte key[ AES::DEFAULT_KEYLENGTH ];    
      prng.GenerateBlock( key, sizeof(key) );        
  
      byte iv[ AES::BLOCKSIZE];    
      iv[AES::BLOCKSIZE] = 0;    
      //prng.GenerateBlock(iv,  sizeof(iv) );        
  
      string plain = "CBC Mode Test";    
      string cipher, encoded, recovered;       
  
      // Pretty print key    
      encoded.clear();    
      StringSource( key, sizeof(key), true,    
                    new HexEncoder(new StringSink( encoded )) // HexEncoder    
      ); // StringSource
  
      cout &lt;&lt; "key: " &lt;&lt; encoded &lt;&lt; endl;        
  
      // Pretty print iv    
      encoded.clear();
  
      StringSource( iv, sizeof(iv), true,    
          new HexEncoder(new StringSink( encoded )) // HexEncoder    
      ); // StringSource
  
      cout &lt;&lt; "iv: " &lt;&lt; encoded &lt;&lt; endl;       
  
      /*********************************\
      \*********************************/
  
      try    
      {    
          cout &lt;&lt; "plain text: " &lt;&lt; plain &lt;&lt; endl;            
          CBC_Mode&lt; AES &gt;::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    
      }    
      catch( CryptoPP::Exception&amp; e )    
      {    
          cerr &lt;&lt; "Caught Exception..." &lt;&lt; endl;    
          cerr &lt;&lt; e.what() &lt;&lt; endl;    
          cerr &lt;&lt; endl;    
      }    
  
      /*********************************\    
      \*********************************/    
  
      // Pretty print    
      encoded.clear();    
      StringSource( cipher, true,    
          new HexEncoder(    
              new StringSink( encoded )    
          ) // HexEncoder    
      ); // StringSource    
      cout &lt;&lt; "cipher text: " &lt;&lt; encoded &lt;&lt; endl;    
  
      /*********************************\    
      \*********************************/    
  
      try    
      {    
          CBC_Mode&lt; AES &gt;::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    
  
          cout &lt;&lt; "recovered text: " &lt;&lt; recovered &lt;&lt; endl;    
      }    
      catch( CryptoPP::Exception&amp; e )    
      {    
          cerr &lt;&lt; "Caught Exception..." &lt;&lt; endl;    
          cerr &lt;&lt; e.what() &lt;&lt; endl;    
          cerr &lt;&lt; endl;    
      }    
  
      /*********************************\    
      \*********************************/    
  
      assert( plain == recovered );    
  
      return 0;    
  }
  </code></pre>