title: Python AES Encryption padding causing issues in C# Decrypting
body: <p>I'm having a bit of an issue encrypting / decrypting cross languages.</p>
  
  <p>Here is my python code to encrypt some text:</p>
  
  <pre><code>class AESCipher:
      def __init__( self, key, iv ):
          self.key = base64.b64decode(key)
          self.iv = base64.b64decode(iv)
      def encrypt( self, raw ):
  
          BS = 16
          pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
          raw = pad(raw)
  
  
          cipher = AES.new( self.key, AES.MODE_CBC, self.iv)
          res = self.iv + cipher.encrypt( raw )
          return base64.b64encode(res) 
  
      def decrypt( self, enc ):
          enc = base64.b64decode(enc)
          unpad = lambda s : s[:-ord(s[len(s)-1:])]
          cipher = AES.new(self.key, AES.MODE_CBC, self.iv )
          return unpad(cipher.decrypt( enc[16:] ))
  
  crypt = AESCipher("key", "iv")
  print "{0}".format(crypt.encrypt("Hallow"))
  </code></pre>
  
  <p>And C# Decrypting:</p>
  
  <pre><code>public static string DecryptStringFromBase64(string base64String)
  {
      byte[] bytes = Decrypt(Convert.FromBase64String(base64String));
      var utf8 = Encoding.UTF8.GetString(bytes);
      return utf8;
  }
  
  public static byte[] Decrypt(byte[] bytes)
  {
      AesManaged algorithm = new AesManaged();
      algorithm.IV = Convert.FromBase64String("IV");
      algorithm.Key = Convert.FromBase64String("KEY");
  
      byte[] ret = null;
      using (var decryptor = algorithm.CreateDecryptor())
      {
          using (MemoryStream msDecrypted = new MemoryStream())
          {
              using (CryptoStream csEncrypt = new CryptoStream(msDecrypted, decryptor, CryptoStreamMode.Write))
              {
                  csEncrypt.Write(bytes, 0, bytes.Length);
              }
              ret = msDecrypted.ToArray();
          }
      }
      return ret;
  }
  </code></pre>
  
  <p>However the decrypted value always comes out incorrectly like this:</p>
  
  <p><a href="https://i.stack.imgur.com/m2kjC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m2kjC.png" alt="decrypted text with garbage at the start"></a></p>
  
  <p>I think this has something to do with the padding, can anybody suggest how to get around this?</p>