3

I have following Powershell-Code to encrypt my password

$my_secure_password = convertto-securestring "myPW" -asplaintext -force $my_encrypted_string = convertfrom-securestring $my_secure_password -key(1..16)

The Output is something like this 76492d1116743f0423413b16050a5345MgB8AHMASgB1AFMARwBxAFMAYgA1AFIAYwAyAE4AOABqAHMAOQBaADgAbwBGAEEAPQA9AHwANwBk................

How can I decrypt this output in C# in order to use it as a SecureString in C#?

I've tested something like this but this didn't work for me

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");

But a is null....

UPDATE I have found a solution for this:

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

1 Answer 1

1

You may try this (maybe it help for you - for me - it is work):

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();

            }
        }
Sign up to request clarification or add additional context in comments.

6 Comments

When I do this str is = System.Security.SecureString But how can I use this then in var credentials = new PSCredential(Username, new_sec_pass); ?
Ok. Can you do this? : foreach (var str in results) { Console.WriteLine(Convert.ToString(str)); Console.ReadKey(); }
My Script looks like this: instance.AddScript("$decryptedpw = convertto-securestring -key (1..16) -string 76492d1.....; $decryptedpw").AddCommand("out-string"); var psOutput = instance.Invoke(); foreach (var result in psOutput) { Console.WriteLine(result); Console.WriteLine(Convert.ToString(result)); Both same Output: System.Security.SecureString
Yes. Sorry. =). You can convert it. I edit my answer. If you run this code - it will converting you security string to unsecurity password
Thank you. Look for the update in my first post, I have found another solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.