我有以下Powershell-Code来加密我的密码
$my_secure_password = convertto-securestring "myPW" -asplaintext -force
$my_encrypted_string = convertfrom-securestring $my_secure_password -key(1..16)
输出是这样的 76492d1116743f0423413b16050a5345MgB8AHMASgB1AFMARwBxAFMAYgA1AFIAYwAyAE4AOABqAHMAOQBaADgAbwBGAEEAPQA9AHwANwBk ................
如何在C#中解密此输出以便在C#中将其用作SecureString?
我测试了类似的东西,但这对我不起作用
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");
但是a是null ....
更新 我找到了解决方案:
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);
答案 0 :(得分:0)
你可以试试这个(也许它对你有帮助 - 对我来说 - 这是工作):
using System.Collections.ObjectModel;
using System.Management.Automation;
static void Main(string[] args)
{
var variable = PowerShell.Create().AddScript("$xtr = convertto-securestring -key (1..16) -string" +
" '76492d1116743f0423413b16050a5345MgB8AEQAbQBIAEIASQB6ADcAeQA0AH" +
"UAaAB6AFEAawBJAEYAVAA0AHcAUwBoAGcAPQA9AHwAMQAzAGUANwBmADIAOAAxADEANwAwAGUAZQBjADMANwBlAG" +
"QAYQAxADcAZABlADEAMQBhAGYAZgBiADkAOQBiADIAMAAwAGMAMgA1AGEAZgAxADcAMABhADAAYwBjAGIAMQBhADAAZAAwADMAMwAzADMAOQAwADcAYQAzAGMANwA='" +
";$PwdPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($xtr);"+
"$PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PwdPointer);$PlainTextPassword").AddCommand("out-string");
Collection<PSObject> results = variable.Invoke();
foreach (var str in results)
{
Console.WriteLine(str);
Console.ReadKey();
}
}