Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net .net安全字符串连接_Vb.net_String Concatenation_Securestring - Fatal编程技术网

Vb.net .net安全字符串连接

Vb.net .net安全字符串连接,vb.net,string-concatenation,securestring,Vb.net,String Concatenation,Securestring,我需要vb.net中字符串合并的帮助。 是否可以将2个安全字符串连接在一起 我有part1.securestring和part2.securesting,我希望我的输出是mainPassword=part1+part2 但它不起作用。 你有什么办法解决这个问题吗?谢谢您的帮助。关闭。 我找到了解决办法: Dim stringPart1 As String Dim stringPart2 As String Dim stringPart3 As String stringPart1 = New

我需要vb.net中字符串合并的帮助。 是否可以将2个安全字符串连接在一起

我有part1.securestring和part2.securesting,我希望我的输出是mainPassword=part1+part2

但它不起作用。 你有什么办法解决这个问题吗?谢谢您的帮助。

关闭。 我找到了解决办法:

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   

如果您首先将
SecureString
s转换为
String
s,这将很容易做到,但这违背了
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

你好欢迎使用stackoverflow,请阅读并提供您的代码,并详细解释哪些代码不起作用。“不工作”并没有告诉我们您期望发生什么以及实际发生了什么。您正在解密字符串并将其作为纯文本存储在内存中。这违背了
SecureString
的目的。这些
stringPart1
等将在处理完后保留在内存中。
Dim result = SecureString1.Append(SecureString2)