title: Decrypting AES CBC data from PyCrypto with Powershell
body: <p>I am working on a project where I will be encrypting a string of data using the AES module from PyCrypto and then decrypting it using Powershell.</p>
  
  <p>I've written a simple encryption function to do what I need here:</p>
  
  <pre><code>import base64
  
  from Crypto import Random
  from Crypto.Cipher import AES
  
  key = "SuperSecret" #Insecure and just for testing
  plaintext = "Secret message please don't look"
  
  BS = 16
  pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
  
  def padKey(s): #Pad key to 32 bytes for AES256
      return (s * (int(32/len(s))+1))[:32]
  
  class AESCipher:
  
      def __init__(self, key):
          self.key = key
  
      def encrypt(self, raw):
          raw = pad(raw)
          iv = Random.new().read( AES.block_size )
          cipher = AES.new( self.key, AES.MODE_CBC, iv )
          return base64.b64encode( iv + cipher.encrypt( raw ) )
  
  paddedKey = padKey(key)
  cipher = AESCipher(paddedKey)
  
  encrypted = str(cipher.encrypt(plaintext))
  encrypted = encrypted[2:-1]
  
  print("Key:", paddedKey)
  print("Plaintext:",plaintext)
  print("Encrypted and B64:",encrypted)
  </code></pre>
  
  <p>I am having some issues with decrypting and decoding the output with Powershell and could use some help. I was able to find a simple decryption script that I've been working with online, but the output is all garbage:</p>
  
  <pre><code>function Create-AesManagedObject($key, $IV) {
      $aesManaged = New-Object "System.Security.Cryptography.AesManaged"
      $aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
      $aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
      $aesManaged.BlockSize = 128
      $aesManaged.KeySize = 256
      if ($IV) {
          if ($IV.getType().Name -eq "String") {
              $aesManaged.IV = [System.Convert]::FromBase64String($IV)
          }
          else {
              $aesManaged.IV = $IV
          }
      }
      if ($key) {
          if ($key.getType().Name -eq "String") {
              $aesManaged.Key = [System.Convert]::FromBase64String($key)
          }
          else {
              $aesManaged.Key = $key
          }
      }
      $aesManaged
  }
  
  function Decrypt-String($key, $encryptedStringWithIV) {
      $bytes = [System.Convert]::FromBase64String($encryptedStringWithIV)
      $IV = $bytes[0..15]
      $aesManaged = Create-AesManagedObject $key $IV
      $decryptor = $aesManaged.CreateDecryptor();
      $unencryptedData = $decryptor.TransformFinalBlock($bytes, 16, $bytes.Length - 16);
      $aesManaged.Dispose()
      [System.Text.Encoding]::UTF8.GetString($unencryptedData).Trim([char]0)
  }
  </code></pre>
  
  <p>Sample output:</p>
  
  <pre><code>PS C:\&gt; Decrypt-String 'SuperSecretSuperSecretSuperSecre' $encryptedString
  ���H�'G zM۞� �i�ZtCI���H~N�GG��A�Pc��aF��`)��GS�N�2{�[.
  </code></pre>
  
  <p>Related: <a href="https://stackoverflow.com/questions/48292315/using-powershell-to-decrypt-a-python-encrypted-string">Using PowerShell to decrypt a Python encrypted String</a></p>