title: Decrypt SecureString from Powershell with C#
body: <p>I have following Powershell-Code to encrypt my password</p>
  
  <p><code>$my_secure_password = convertto-securestring "myPW" -asplaintext -force
   $my_encrypted_string = convertfrom-securestring $my_secure_password -key(1..16)
  </code></p>
  
  <p>The Output is something like this
  76492d1116743f0423413b16050a5345MgB8AHMASgB1AFMARwBxAFMAYgA1AFIAYwAyAE4AOABqAHMAOQBaADgAbwBGAEEAPQA9AHwANwBk................</p>
  
  <p>How can I decrypt this output in C# in order to use it as a SecureString in C#?</p>
  
  <p>I've tested something like this but this didn't work for me</p>
  
  <pre><code>var runSpace_new = RunspaceFactory.CreateRunspace();
  runSpace_new.Open();
  
  PowerShell PSinstance = PowerShell.Create();
  PSinstance.AddScript("$decrypt = convertto-securestring -key (1..16) -string 76492d1116743f0423413b.......");
  var psOutput_new = PSinstance.Invoke();
  var a = runSpace_new.SessionStateProxy.PSVariable.GetValue("decrypt");
  </code></pre>
  
  <p>But a is null....</p>
  
  <p><strong>UPDATE</strong>
  I have found a solution for this:</p>
  
  <pre><code>SecureString new_sec_pass = new SecureString();
  PowerShell instance = PowerShell.Create();
  
  instance.AddScript("convertto-securestring -key (1..16) -string 76492d1...");
  
  foreach (PSObject psOutput in instance.Invoke())
  {
      new_sec_pass = psOutput.BaseObject as SecureString;
  }
  
  PSCredential new_credentials = new PSCredential(Username, new_sec_pass);
  </code></pre>