如何解密通过PowerShell加密的C#中的字符串 [英] How to decrypt a string in C# which is encrypted via PowerShell

查看:210
本文介绍了如何解密通过PowerShell加密的C#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以解密通过 PowerShell 加密的C#中的字符串,如何?



该字符串通过PowerShell加密如下:

  $ pw = read-host输入密码--AsSecureString 

ConvertFrom-SecureString $ pw | out-fileC:\file.txt

要使用PowerShell将其转换回来,我可以使用这些命令调用C#类 System.Runtime.InteropServices.Marshal

  $ pwdSec = Get-ContentC:\file.txt| ConvertTo-SecureString 

$ bPswd = [System.Runtime.InteropServices.Marshal] :: SecureStringToBSTR($ pwdSec)

$ pswd = [System.Runtime.InteropServices.Marshal] :: PtrToStringAuto($ bPswd)

文件包含已转换为加密标准的字符串字符串( 你好)



所以如果打开 file.txt 文件,它看起来类似于:



<预类= 朗 - 无prettyprint-越权> 01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000
000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e000000000480
0000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2
d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f37
22c14c69684841b6b21c


解决方案

ConvertFrom-SecureString 你是一个UTF-16(密码)字符串,保护用 ProtectedData.Protect 保存为十六进制转储。



确定还原编码的使用:

  //将文件读入字符串
string exportedData = File。 ReadAllText(@ file.txt的);

//删除所有新行
exportedData = exportedData.Replace(Environment.NewLine,);

//将十六进制转储转换为字节数组
int length = exportedData.Length / 2;
byte [] encryptedData = new byte [length]; (int index = 0; index< length; ++ index)
{
encryptedData [index] =
byte.Parse(
exportedData.Substring( 2 * index,2),
NumberStyles.HexNumber,CultureInfo.InvariantCulture);
}

//将字节数组解密为Unicode字节数组
byte [] data =
ProtectedData.Unprotect(encryptedData,(byte [])null,DataProtectionScope 。当前用户);

//将Unicode字节数组转换为字符串
string password = Encoding.Unicode.GetString(data);






上述代码可以工作,当您不指定 -keyeye / rel =nofollow noreferrer> ConvertFrom-SecureString 。然后,使用 Windows数据保护API(DPAPI)保护安全字符串。因此,字符串必须在同一机器和帐户上进行解码,就像编码一样。


Is it possible to decrypt a string in C# which is encrypted via PowerShell and how?

The string is encrypted via PowerShell as below:

$pw = read-host "Enter Password" –AsSecureString

ConvertFrom-SecureString $pw | out-file "C:\file.txt"

To convert it back with PowerShell I can use these commands that call C# class System.Runtime.InteropServices.Marshal.

$pwdSec = Get-Content "C:\file.txt" | ConvertTo-SecureString

$bPswd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwdSec)

$pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd)

File contains the string that has been converted to an encrypted standard string("hello").

So if open the file.txt file, it looks similar to:

01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000
000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e000000000480
0000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2
d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f37
22c14c69684841b6b21c

解决方案

The output file from the ConvertFrom-SecureString you have is a UTF-16 (password) string protected with the ProtectedData.Protect stored as a hex dump.

Do revert the encoding use:

// Read file to string
string exportedData = File.ReadAllText(@"file.txt");

// Remove all new-lines
exportedData = exportedData.Replace(Environment.NewLine, "");

// Convert the hex dump to byte array
int length = exportedData.Length / 2;
byte[] encryptedData = new byte[length];
for (int index = 0; index < length; ++index)
{
    encryptedData[index] =
        byte.Parse(
            exportedData.Substring(2 * index, 2),
            NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}

// Decrypt the byte array to Unicode byte array
byte[] data =
    ProtectedData.Unprotect(encryptedData, (byte[])null, DataProtectionScope.CurrentUser);

// Convert Unicode byte array to string
string password = Encoding.Unicode.GetString(data);


The above code works, when you do not specify the -Key with the ConvertFrom-SecureString. The secure string is then protected with Windows Data Protection API (DPAPI). As such the string has to be decoded on the same machine and account, as it was encoded.

这篇关于如何解密通过PowerShell加密的C#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆