title: Python to C# AES CBC PKCS7
body: <p>I'm trying to convert this C# code to Python (2.5, GAE). The problem is that the encrypted string from the python script is different each time the encryption (on the same string) is run.</p>
  
  <pre><code>string Encrypt(string textToEncrypt, string passphrase)
   {
      RijndaelManaged rijndaelCipher = new RijndaelManaged();
      rijndaelCipher.Mode = CipherMode.CBC;
      rijndaelCipher.Padding = PaddingMode.PKCS7;
  
      rijndaelCipher.KeySize = 128;
      rijndaelCipher.BlockSize = 128;
      byte[] pwdBytes = Encoding.UTF8.GetBytes(passphrase);
      byte[] keyBytes = new byte[16];
      int len = pwdBytes.Length;
      if (len &gt; keyBytes.Length)
      {
          len = keyBytes.Length;
      }
      Array.Copy(pwdBytes, keyBytes, len);
      rijndaelCipher.Key = keyBytes;
      rijndaelCipher.IV = new byte[16];
      ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
      byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
      return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
  }
  </code></pre>
  
  <p>Python code: (PKCS7Encoder: <a href="http://japrogbits.blogspot.com/2011/02/using-encrypted-data-between-python-and.html" rel="noreferrer">http://japrogbits.blogspot.com/2011/02/using-encrypted-data-between-python-and.html</a>)</p>
  
  <pre><code>from Crypto.Cipher import AES
  from pkcs7 import PKCS7Encoder
  #declared outside of all functions
  key = '####'
  mode = AES.MODE_CBC
  iv = '\x00' * 16
  encryptor = AES.new(key, mode, iv)
  encoder = PKCS7Encoder()
  
  def function(self):
   text = self.request.get('passwordTextBox')
   pad_text = encoder.encode(text)
   cipher = encryptor.encrypt(pad_text)
   enc_cipher = base64.b64encode(cipher)
  </code></pre>
  
  <p>The C# code is inherited. Python code must be encrypted and decrypted the same way so that the C# code can decode the value correctly.</p>
  
  <p>Note: I am a noob at python :)</p>
  
  <p>Edit: sorry. should have made the distinction that there was a function being called.</p>
  
  <p>Thanks!</p>