title: powershell asymmetric encrypt/decrypt functions
body: <p>I wasn't able to find good powershell functions to utilize asymmetric encryption so I created the following.  Would like any feedback in terms of improvement as I'm a crypto noob.  With the caveat that these functions are <em>very</em> basic.  There isn't error checking and the write-host after a decrypt is hardly necessary.  Just want to establish the core functionality before adding things like protected memory and such.</p>
  
  <p>This has been successfully tested on two systems: Win8 w/Powershell v3 &amp; Win2008R2 w/Powershell v2.</p>
  
  <pre><code>Function Encrypt-Asymmetric([string]$Encrypt,[string]$CertPath,[string]$XmlExportPath)
  {
      # Encrypts a string with a public key
      $pubcer = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertPath)
      $byteval = [System.Text.Encoding]::UTF8.GetBytes($Encrypt)
      $pubcer.PublicKey.Key.Encrypt($byteval,$true) | Export-Clixml -Path $XmlExportPath    
  }
  
  Function Decrypt-Asymmetric([string]$XmlPath,[string]$CertThumbprint)
  {
      # Decrypts cipher text using the private key
      # Assumes the certificate is in the LocalMachine store
      $store = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
      $store.open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
      $cer = $store.Certificates | %{if($_.thumbprint -eq $CertThumbprint){$_}}
      $ciphertext = Import-Clixml -Path $XmlPath
      $decryptedBytes = $cer.PrivateKey.Decrypt($ciphertext,$true)
      $ClearText = [System.Text.Encoding]::UTF8.GetString($decryptedBytes)
      Write-Host $ClearText
  }
  </code></pre>