解密vb.net中的字符串数组

时间:2013-11-07 12:21:58

标签: arrays vb.net encryption

我有一个现有程序,逐行加密和解密文本文件(应用程序中的其他功能随机将加密行附加到文件中,因此您不能只加密整个文件)。

偶尔我需要搜索整个加密文件以查找某个字符串,所以此刻我将文件的所有行读入一个数组,然后循环遍历数组,将每个元素传递给解密函数,这样我就可以相应地搜索(下面的示例代码)。

 Dim objReader As New System.IO.StreamReader("my file")

    Dim decryptedarray As New ArrayList
    Do While objReader.Peek() <> -1
        decryptedarray.Add(DecryptString(objReader.ReadLine(), SystemKey))
    Loop

我的主要问题是一些文件可能长约20000行,而将行读入内存只需要几分之一秒,迭代数组并解密每个元素的过程大约需要60秒用户需要等待很长时间。

我想知道是否有更有效的方法对数组的所有元素执行操作,而不仅仅是循环遍历它以尝试提高速度。或者,如果任何人可以提供更有效的解密方法(目前的方法在下面供您阅读),或者事实上任何关于如何提高速度的评论都是非常受欢迎的。

  Public Function DecryptString(ByVal vstrStringToBeDecrypted As String, _
                                    ByVal vstrDecryptionKey As String) As String

    Dim test As String = Len(vstrStringToBeDecrypted)
    Dim holder As String = vstrStringToBeDecrypted
    If Len(vstrStringToBeDecrypted) > 1 Then
        Dim bytDataToBeDecrypted() As Byte
        Dim bytTemp() As Byte
        Dim bytIV() As Byte = {some byte IV}
        Dim objRijndaelManaged As New RijndaelManaged()
        Dim objMemoryStream As MemoryStream
        Dim objCryptoStream As CryptoStream
        Dim bytDecryptionKey() As Byte

        Dim intLength As Integer
        Dim intRemaining As Integer

        Dim strReturnString As String = String.Empty

        Dim stringtodecrypt As String = LoadDBString(vstrStringToBeDecrypted)
        If Not stringtodecrypt = "" Then

            Try
                bytDataToBeDecrypted = Convert.FromBase64String(stringtodecrypt)
            Catch ex As Exception
                Return ""
                Exit Function
            End Try

            intLength = Len(vstrDecryptionKey)

            If intLength >= 32 Then
                vstrDecryptionKey = Strings.Left(vstrDecryptionKey, 32)
            Else
                intLength = Len(vstrDecryptionKey)
                intRemaining = 32 - intLength
                vstrDecryptionKey = vstrDecryptionKey & Strings.StrDup(intRemaining, "X")
            End If

            bytDecryptionKey = Encoding.ASCII.GetBytes(vstrDecryptionKey.ToCharArray)

            ReDim bytTemp(bytDataToBeDecrypted.Length)

            objMemoryStream = New MemoryStream(bytDataToBeDecrypted)

            Try

                objCryptoStream = New CryptoStream(objMemoryStream, _
                   objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), _
                   CryptoStreamMode.Read)

                objCryptoStream.Read(bytTemp, 0, bytTemp.Length)

                objCryptoStream.FlushFinalBlock()
                objMemoryStream.Close()
                objCryptoStream.Close()

            Catch

            End Try

            Return StripNullCharacters(Encoding.ASCII.GetString(bytTemp))
        Else
            Return ""
        End If

    Else
        Return ""
    End If

End Function

0 个答案:

没有答案