当前位置: 首页 > 编程语言 > C#

如何在C#中解密通过PowerShell加密的字符串

时间:2023-04-11 02:00:02 C#

如何在C#中解密通过PowerShell加密的字符串该字符串通过PowerShell加密,如下所示:$pw=read-host"EnterPassword"–AsSecureStringConvertFrom-SecureString$pw|out-file"C:file.txt"要使用PowerShell将其转换回来,我可以使用这些调用C#类System.Runtime.InteropServices.Marshal命令。$pwdSec=获取内容"C:file.txt"|ConvertTo-SecureString$bPswd=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwdSec)$pswd=[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd)文件包含转换为加密的标准字符串("你好”)。因此,如果打开file.txt文件,它看起来类似于:01000000d08c9ddf0115d1118c7a00c04fc297eb0100000052ded6c2db80e748933432e19b9de8b10000000002000000000003660000c00000001000000016dc35885d76d07bab289eb9927cfc1e0000000004800000a0000000100000003106cde553f45b08d13d89d11336170b280000005cc865c1ee1b57e84ed3d1a2d3f2d0ec0f189b532e61c18d1f31444d6f119a1e8368477fd2d81f54140000000cb0262e58b08ae14f3722c14c69684841b6b21c您拥有的ConvertFrom-SecureString的输出文件是一个UTF-16(密码)字符串,受ProtectedData.Protect存储为hex转储。Pleaserestoreencodingusing://ReadfiletostringstringexportedData=File.ReadAllText(@"file.txt");//Removeallnew-linesexportedData=exportedData.Replace(Environment.NewLine,"");//Convertthehexdumptobytearrayintlength=exportedData.Length/2;byte[]encryptedData=newbyte[length];for(intindex=0;indexTheabovecodeworksfinewhenyoudon'tspecify-KeywithConvertFrom-SecureStringworks.ThesecurestringisthenprotectedusingtheWindowsDataProtectionAPI(DPAPI).Therefore,thestringmustbedecodedonthesamemachineandaccountasitwasencoded.以上就是C#学习教程:如何解密C#中PowerShell加密的字符串的所有分享内容,如果对大家有用还需要详细了解C#学习教程,希望大家多多关注---本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: