我需要在vb.net中合并字符串的帮助。可以将两个安全字符串连接在一起吗?
我有part1.securestring和part2.securesting,我希望我的输出是mainPassword = part1 +part2。
但它不起作用。你有什么办法解决这个问题吗?谢谢你的帮助。
发布于 2018-06-19 15:49:27
如果您首先将SecureString转换为String,这将很容易做到,但这违背了SecureString的真正目的,即不将敏感信息作为挂起的字符串对象留在内存中。您必须小心,仅使用字节数组,并在之后将其清零。
<Extension()> _
Public Function Append(ByVal s1 As SecureString, ByVal s2 As SecureString) As SecureString
Dim b() As Byte
Dim p1 = Marshal.SecureStringToGlobalAllocUnicode(s1)
Try
Dim p2 = Marshal.SecureStringToGlobalAllocUnicode(s2)
Try
ReDim b(0 To s1.Length * 2 + s2.Length * 2 - 1)
Marshal.Copy(p1, b, 0, s1.Length * 2)
Marshal.Copy(p2, b, s1.Length * 2, s2.Length * 2)
Finally
Marshal.ZeroFreeGlobalAllocUnicode(p2)
End Try
Finally
Marshal.ZeroFreeGlobalAllocUnicode(p1)
End Try
Dim res = New SecureString()
For i As Integer = LBound(b) To UBound(b) Step 2
res.AppendChar(BitConverter.ToChar(b, i))
Next
res.MakeReadOnly()
Array.Clear(b, 0, b.Length)
Return res
End Function用法:
Dim result = SecureString1.Append(SecureString2)发布于 2018-06-19 15:43:16
结案。我找到了解决方案:
Dim stringPart1 As String
Dim stringPart2 As String
Dim stringPart3 As String
stringPart1 = New System.Net.NetworkCredential(String.Empty,part1).Password
stringPart2 = New System.Net.NetworkCredential(String.Empty,part2).Password
stringPart3 = New System.Net.NetworkCredential(String.Empty,part3).Password
hasloGlowne = New Security.SecureString()
For Each c As Char In stringpart1
hasloGlowne.AppendChar(c)
Next
For Each c As Char In stringpart2
hasloGlowne.AppendChar(c)
Next
For Each c As Char In stringpart3
hasloGlowne.AppendChar(c)
Next https://stackoverflow.com/questions/50922583
复制相似问题