title: go AES encryption CBC
body: <h1>Problem</h1>
  <p>I have to port a function from <code>C#</code> to <code>GO</code>, which is using <code>AES</code> encryption.
  obviously i have to get the same result with <code>GO</code> that i get with <code>C#</code></p>
  <h1>C#</h1>
  <h2>code fiddle</h2>
  <p>I prepared a small <a href="https://dotnetfiddle.net/b7y0a5" rel="nofollow noreferrer">fiddle</a> with <code>C#</code></p>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Linq;
  using System.Security.Cryptography;
  using System.Text;
  
  public class Program
  {
      public static void Main()
      {
          var query = &quot;csharp -&gt; golang&quot;;
          var key = Encoding.UTF8.GetBytes(&quot;12345678901234567890123456789012&quot;);
          var iv = Encoding.UTF8.GetBytes(&quot;1234567890123456&quot;);
          using (var aes = (RijndaelManaged)RijndaelManaged.Create())
          {
              aes.KeySize = 256;
              aes.Mode = CipherMode.CBC;
              aes.Key = key;
              aes.IV = iv;
              using (var transform = aes.CreateEncryptor())
              {
                  Console.WriteLine(&quot;query =&gt; &quot; + query);
                  var toEncodeByte = Encoding.UTF8.GetBytes(query);
                  Console.WriteLine(&quot;toEncodeByte = &quot; + ToString(toEncodeByte));
                  var encrypted = transform.TransformFinalBlock(toEncodeByte, 0, toEncodeByte.Length);
                  Console.WriteLine(&quot;encrypted = &quot; + ToString(encrypted));
              }
          }
      }
  
      public static string ToString(byte[] b)
      {
          return &quot;[&quot; + String.Join(&quot; &quot;, b.Select(h =&gt; h.ToString())) + &quot;]&quot;;
      }
  }
  </code></pre>
  <h2>console output</h2>
  <pre class="lang-sh prettyprint-override"><code>query =&gt; csharp -&gt; golang
  toEncodeByte = [99 115 104 97 114 112 32 45 62 32 103 111 108 97 110 103]
  encrypted = [110 150 8 224 44 118 15 182 248 172 105 14 61 212 219 205 216 31 76 112 179 76 214 154 227 112 159 176 24 61 108 100]
  </code></pre>
  <h1>GO</h1>
  <h2>code fiddle</h2>
  <p>i prepared a small <a href="https://play.golang.org/p/bWPvYudlwbv" rel="nofollow noreferrer">fiddle</a> wit <code>GO</code></p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;bytes&quot;
      &quot;crypto/aes&quot;
      &quot;crypto/cipher&quot;
      &quot;encoding/hex&quot;
      &quot;fmt&quot;
  )
  
  func main() {
      query := &quot;csharp -&gt; golang&quot;
      key := []byte(&quot;12345678901234567890123456789012&quot;)
      iv := []byte(&quot;1234567890123456&quot;)
  
      if len(key) != 32 {
          fmt.Printf(&quot;key len must be 16. its: %v\n&quot;, len(key))
      }
      if len(iv) != 16 {
          fmt.Printf(&quot;IV len must be 16. its: %v\n&quot;, len(iv))
      }
      var encrypted string
      toEncodeByte := []byte(query)
      fmt.Println(&quot;query =&gt;&quot;, query)
      fmt.Println(&quot;toEncodeByte = &quot;, toEncodeByte)
      toEncodeBytePadded := PKCS5Padding(toEncodeByte, len(key))
  
      // aes
      block, err := aes.NewCipher(key)
      if err != nil {
          fmt.Println(&quot;CBC Enctryping failed.&quot;)
      }
      ciphertext := make([]byte, len(toEncodeBytePadded))
      mode := cipher.NewCBCEncrypter(block, iv)
      mode.CryptBlocks(ciphertext, toEncodeBytePadded)
      encrypted = hex.EncodeToString(ciphertext)
      // end of aes
      fmt.Println(&quot;encrypted&quot;, []byte(encrypted))
  }
  
  func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
      padding := (blockSize - len(ciphertext)%blockSize)
      padtext := bytes.Repeat([]byte{byte(padding)}, padding)
      return append(ciphertext, padtext...)
  }
  </code></pre>
  <h2>console output</h2>
  <pre class="lang-sh prettyprint-override"><code>query =&gt; csharp -&gt; golang
  toEncodeByte =  [99 115 104 97 114 112 32 45 62 32 103 111 108 97 110 103]
  encrypted [54 101 57 54 48 56 101 48 50 99 55 54 48 102 98 54 102 56 97 99 54 57 48 101 51 100 100 52 100 98 99 100 100 56 49 102 52 99 55 48 98 51 52 99 100 54 57 97 101 51 55 48 57 102 98 48 49 56 51 100 54 99 54 52]
  </code></pre>
  <h1>Summary</h1>
  <p>I noticed that in the <code>C#</code> the input does not have to be same size as <code>block</code>.<br />
  In <code>GO</code> this seems not to work without padding (therefore I added Padding in the <code>GO</code>code) but the result is different.<br />
  An solution to gain an <code>equal</code> result would be great</p>