title: python AES Encrypt
body: <p>I am new in python/django. I want to use AES Encryption.
  after search I found some libraries like this: <a href="https://gist.github.com/jeetsukumaran/1291836" rel="nofollow noreferrer">https://gist.github.com/jeetsukumaran/1291836</a>
  but I could not use them because of I have IV but this dont have IV code.
  I have sample code in c#. any one may help me to convert this code to python code?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Security.Cryptography;
  using System.IO;
  namespace AESUtility {
   public class AES {
    string AES_Key = string.Empty;
    string AES_IV = string.Empty;
    public AES(string AES_Key, string AES_IV) {
     this.AES_Key = AES_Key;
     this.AES_IV = AES_IV;
    }
    public bool Encrypt(String Input, out string encryptedString) {
     try {
      var aes = new RijndaelManaged();
      aes.KeySize = 256;
      aes.BlockSize = 256;
      aes.Padding = PaddingMode.PKCS7;
      aes.Key = Convert.FromBase64String(this.AES_Key);
      aes.IV = Convert.FromBase64String(this.AES_IV);
      var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
      byte[] xBuff = null;
      using(var ms = new MemoryStream()) {
       using(var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) {
        byte[] xXml = Encoding.UTF8.GetBytes(Input);
        cs.Write(xXml, 0, xXml.Length);
       }
       xBuff = ms.ToArray();
      }
      encryptedString = Convert.ToBase64String(xBuff);
      return true;
     } catch (Exception ex) {
      encryptedString = string.Empty;
      return false;
     }
    }
    public bool Decrypt(String Input, out string decodedString) {
     try {
      RijndaelManaged aes = new RijndaelManaged();
      aes.KeySize = 256;
      aes.BlockSize = 256;
      aes.Mode = CipherMode.CBC;
      aes.Padding = PaddingMode.PKCS7;
      aes.Key = Convert.FromBase64String(this.AES_Key);
      aes.IV = Convert.FromBase64String(this.AES_IV);
      var decrypt = aes.CreateDecryptor();
      byte[] xBuff = null;
      using(var ms = new MemoryStream()) {
       using(var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) {
        byte[] xXml = Convert.FromBase64String(Input);
        cs.Write(xXml, 0, xXml.Length);
       }
       xBuff = ms.ToArray();
      }
      decodedString = Encoding.UTF8.GetString(xBuff);
      return true;
     } catch (Exception ex) {
      decodedString = string.Empty;
      return false;
     }
    }
   }
  }
  </code></pre>