title: C ++ (crypto ++) and C # encryption matching
body: <p>For secure communication between client and server, I want to encrypt and decrypt data from both the client and the server, but I cannot get the same results, the data that comes from the client to the server is not decrypted, also when encrypting the same the same data from the server, I get a distinctive result from encryption on the client.</p>
  <p>The client is written in C ++ using Crypto ++. The code:</p>
  <pre><code>string data = &quot;teststring,teststring,teststring&quot;;
  string encryptedData = client.encrypt(userData, &quot;01234567891234560123456789123456&quot;, &quot;0123456789123456&quot;);
  </code></pre>
  <pre><code>string Client::encrypt(const string&amp; str_in, const string&amp; key, const string&amp; iv)
  {
      try {
          string str_out;
   
          AES::Encryption aesEncryption((byte*)key.c_str(), key.length());
          CBC_Mode_ExternalCipher::Encryption encryption(aesEncryption, (byte*)iv.c_str());
   
          StringSource encryptor(str_in, true,
              new StreamTransformationFilter(encryption,
                  new Base64Encoder(
                      new StringSink(str_out),
                      false
                  )
              )
          );
          return str_out;
      }
      catch (exception e) {
          return &quot;null&quot;;
      }
  }
  string Client::decrypt(const string&amp; str_in, const string&amp; key, const string&amp; iv)
  {
      try {
          string str_out;
          CBC_Mode&lt;AES&gt;::Decryption decryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
   
          StringSource decryptor(str_in, true,
              new Base64Decoder(
                  new StreamTransformationFilter(decryption,
                      new StringSink(str_out)
                  )
              )
          );
   
          return str_out;
      }
      catch (exception e) {
          return &quot;null&quot;;
      }
  }
  </code></pre>
  <p>Gives the following output:</p>
  <pre><code>data: &quot;teststring,teststring,teststring&quot;
  encryptedData: &quot;STpuD/dRgYard+Yqpdd5KOYET7607i7ZRUoKm5eshHzR3ErafaxgZ2+T1tSp0lWJ&quot;
  </code></pre>
  <p>The server side of the code is written in C # (ASP.NET Core 3.1):</p>
  <pre><code>public static string Encrypt(string input)
  {
      byte[] clearBytes = Encoding.Default.GetBytes(input);
   
      using (Aes encryptor = Aes.Create(&quot;AES&quot;))
      {
          // encryptor.BlockSize = 32;
          encryptor.Padding = PaddingMode.Zeros;
          encryptor.KeySize = 128;
          encryptor.Mode = CipherMode.CBC;
          encryptor.Key = Encoding.Default.GetBytes(&quot;01286567891233460123456789123456&quot;);
          encryptor.IV = Encoding.Default.GetBytes(&quot;0123456789123456&quot;);
          using (MemoryStream ms = new MemoryStream())
          {
              using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
              {
                  cs.Write(clearBytes, 0, clearBytes.Length);
                  cs.Close();
              }
              byte[] bt = ms.ToArray();
              input = Convert.ToBase64String(bt);
          }
      }
      return input;
  }
   
  public static string Decrypt(string input)
  {
      byte[] clearBytes = Encoding.ASCII.GetBytes(input);
   
      using (Aes decryptor = Aes.Create(&quot;AES&quot;))
      {
          //decryptor.BlockSize = 32;
          decryptor.Padding = PaddingMode.Zeros;
          decryptor.KeySize = 128;
          decryptor.Mode = CipherMode.CBC;
          decryptor.Key = Encoding.ASCII.GetBytes(&quot;01234567891234560123456789123456&quot;);
          decryptor.IV = Encoding.ASCII.GetBytes(&quot;0123456789123456&quot;);
          using (MemoryStream ms = new MemoryStream())
          {
              using (CryptoStream cs = new CryptoStream(ms, decryptor.CreateDecryptor(), CryptoStreamMode.Write))
              {
                  cs.Write(clearBytes, 0, clearBytes.Length);
                  cs.Close();
              }
              byte[] bt = ms.ToArray();
              input = Encoding.ASCII.GetString(bt);
          }
      }
      return input;
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>data: &quot;teststring,teststring,teststring&quot;
  encryptedData: &quot;tIDlWnnzOQWh5HIvRDJya6z7jOglkIlYrICeYW9RoEM=&quot;
  </code></pre>
  <p>I cannot decrypt data from both the client and the server. Help me please.</p>