0

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

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

 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
4

0 回答 0