title: AES. Encrypt array of bytes in powershell
body: <p>I am need to encrypt array of bytes([byte[]]) in powershell script using AES cryptography method. I am find function that encode string:</p>
  
  <pre><code>[Reflection.Assembly]::LoadWithPartialName("System.Security")
  
  function Encrypt-String($String, $Passphrase, $salt="My Voice is my P455W0RD!",     $init="Yet another key", [switch]$arrayOutput)
  {
     $r = new-Object System.Security.Cryptography.RijndaelManaged
     $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)
     $salt = [Text.Encoding]::UTF8.GetBytes($salt)
  
     $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8
     $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15]
  
     $c = $r.CreateEncryptor()
     $ms = new-Object IO.MemoryStream
     $cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write"
     $sw = new-Object IO.StreamWriter $cs
     $sw.Write($String)
     $sw.Close()
     $cs.Close()
     $ms.Close()
     $r.Clear()
     [byte[]]$result = $ms.ToArray()
     if($arrayOutput) {
    return $result
     } else {
        return [Convert]::ToBase64String($result)
     }
  }
  </code></pre>
  
  <p>Read code with comments at <a href="http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-410ef9df" rel="nofollow">http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-410ef9df</a></p>
  
  <p>Help change the function so that it encode array of bytes and not a string</p>
  
  <p>Ssory for my english. Hello from Russia :)</p>