我正在使用power shell,我有成功将用户输入的密码转换为纯文本的代码:
$SecurePassword = Read-Host -AsSecureString "Enter password" | convertfrom-securestring | out-file C:\Users\tmarsh\Documents\securePassword.txt
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种方法将其转换回来,但它们似乎都没有正常工作.最近,我尝试过以下内容:
$PlainPassword = Get-Content C:\Users\tmarsh\Documents\securePassword.txt
#convert the SecureString object to plain text using PtrToString and SecureStringToBSTR
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PlainPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) #this is an important step to keep things secure
Run Code Online (Sandbox Code Playgroud)
这也给我一个错误.
Cannot convert argument "s", with value: "01000000d08c9ddf0115d1118c7a00c04fc297eb0100000026a5b6067d53fd43801a9ef3f8ef9e43000000000200000000000366000
0c0000000100000008118fdea02bfb57d0dda41f9748a05f10000000004800000a000000010000000c50f5093f3b87fbf9ee57cbd17267e0a10000000833d1d712cef01497872a3457bc8
bc271400000038c731cb8c47219399e4265515e9569438d8e8ed", for "SecureStringToBSTR" to type "System.Security.SecureString": "Cannot convert the "01000000
d08c9ddf0115d1118c7a00c04fc297eb0100000026a5b6067d53fd43801a9ef3f8ef9e430000000002000000000003660000c0000000100000008118fdea02bfb57d0dda41f9748a05f10
000000004800000a000000010000000c50f5093f3b87fbf9ee57cbd17267e0a10000000833d1d712cef01497872a3457bc8bc271400000038c731cb8c47219399e4265515e9569438d8e8
ed" value of type "System.String" to type "System.Security.SecureString"."
At C:\Users\tmarsh\Documents\Scripts\Local Admin Script\PlainTextConverter1.ps1:14 char:1
+ …Run Code Online (Sandbox Code Playgroud) 我需要在一块内存中搜索一串字符,但是这些字符串中有几个将每个字符都空分隔,如下所示:
"I. .am .a .string"
,所有的'.'为null字符.我的问题来自实际上将其记入内存.我尝试了几种方法,例如:
char* str2;
str2 = (char*)malloc(sizeof(char)*40);
memcpy((void*)str2, "123\0567\09abc", 12);
Run Code Online (Sandbox Code Playgroud)
将以下内容放入str2指向的内存中:123.7.9abc..
像
str2 = "123456789\0abcde\054321";
will这样的东西将str2指向一个看起来像的内存块123456789.abcde,321,其中'.' 是一个空字符,','是一个实际的逗号.
因此,清楚地将空字符插入到cstrings中并不像我想象的那样容易,就像插入换行符一样.我在使用字符串库时也遇到了类似的困难.我可以做单独的任务,例如:
char* str;
str = (char*)malloc(sizeof(char)*40);
strcpy(str, "123");
strcpy(str+4, "abc");
strcpy(str+8, "ABC");
Run Code Online (Sandbox Code Playgroud)
但这肯定不是优选的,我相信问题在于我对c样式字符串如何存储在内存中的理解.很明显,"abc\0123"实际上并没有进入内存61 62 63 00 31 32 33(以十六进制表示).它是如何存储的,我如何存储我需要的东西?
(我也为没有设置块的代码而道歉,这是我第一次发布一个问题,并且以某种方式"四个间隔"比我能处理得更困难.谢谢你,Luchian.我看到需要更多新行.)
我想知道我是否采取了任何可能导致代码故障的隐含假设?
我想避免使用 Import-Clixml cmdlet 是有原因的吗?因此,我开发了一种替代方案,即一系列命令,旨在从使用 Export-Clixml 创建的 CliXml 文件中提取用户名和密码。它现在可以工作,但我不确定分割解决方案是否可靠。
$credFileUriBld = [UriBuilder]::New('file','localhost',-1,"MyCredentials.xml"))
$credFile = [Xml.XMLDocument]::New()
$nsMgr4ps1xml = [Xml.XmlNamespaceManager]::New($credFile.NameTable)
$nsMgr4ps1xml.AddNamespace('ps1xml','http://schemas.microsoft.com/powershell/2004/04')
$credFile.Load($credFileUriBld.Path)
$netCredInfo = [System.Net.NetworkCredential]::New($credFile.SelectSingleNode('/ps1xml:Objs/ps1xml:Obj/ps1xml:Props/ps1xml:S[@N=''UserName'']/text()',$nsMgr4ps1xml).Get_Value(),
($credFile.SelectSingleNode('/ps1xml:Objs/ps1xml:Obj/ps1xml:Props/ps1xml:SS[@N=''Password'']/text()',$nsMgr4ps1xml).Get_Value().Split('00') |
ForEach-Object { if([String]::IsNullOrEmpty($_)) { } else { $_.Trim() } } |
ForEach-Object { [convert]::ToInt32($_,16) } |
ForEach-Object { [convert]::ToChar($_) } |
ForEach-Object -Begin { $ss=[SecureString]::New() } -Process {$ss.AppendChar($_)} -End { $ss }))
$netCredInfo.UserName
$netCredInfo.Password
Run Code Online (Sandbox Code Playgroud)
您能否看一下并建议是否存在任何使代码不可靠的假设?
powershell credentials securestring networkcredentials powershell-core
$Variable = Read-Host "Enter thing" -AsSecureString
Run Code Online (Sandbox Code Playgroud)
将提示您输入并将其保存为变量的安全字符串。如何解密安全字符串变量?
PS C:\Users\Todd> $Variable
System.Security.SecureString
Run Code Online (Sandbox Code Playgroud)