尝试解密时,Base-64字符串中的无效字符

| 我有一个加密/解密方法,除了一个例外,它工作得很好。当我尝试从文本文件中读取加密的文本然后解密时,出现以下错误。
Invalid character in a Base-64 string
奇怪的是,如果我只是将加密的文本读入文本框中,然后将其复制并粘贴到另一个文本框中,该文本框将使用工作正常的相同解密方法进行解密。没有错误,解密继续进行。我在下面列出了解密方法和用于读取文本文件的方法。 解密方式
   Public Shared Function DecryptUserString(ByRef cipheredText As String, ByRef password As String) As String
      Dim RijndaelManagedObj As New RijndaelManaged
      Dim RijndaelEncObj As ICryptoTransform, MD5Obj As New MD5CryptoServiceProvider
      Dim DecryptedBytes As Byte(), EncryptedData As Byte()
      Dim PasswordBytes As Byte() = New ASCIIEncoding().GetBytes(password)
      Dim UTF8Encoding As System.Text.Encoding = System.Text.Encoding.UTF8

      \'A modified Base64 is sent with ~ and -  so it can be sent as a form post
      EncryptedData = Convert.FromBase64String(Replace(Replace(cipheredText, \"~\", \"+\"), \"-\", \"=\"))

      RijndaelManagedObj.BlockSize = 128
      RijndaelManagedObj.KeySize = 128
      RijndaelManagedObj.Mode = CipherMode.ECB
      RijndaelManagedObj.Padding = PaddingMode.None
      RijndaelManagedObj.Key = MD5Obj.ComputeHash(PasswordBytes)
      RijndaelEncObj = RijndaelManagedObj.CreateDecryptor()

      DecryptedBytes = RijndaelEncObj.TransformFinalBlock(EncryptedData, 0, EncryptedData.Length)

      If DecryptedBytes.Length > 0 Then
         DecryptUserString = UTF8Encoding.GetString(DecryptedBytes, 0, DecryptedBytes.Length)
         If DecryptedBytes.Length = 0 Then DecryptUserString = New ASCIIEncoding().GetString(DecryptedBytes)
      Else
         DecryptUserString = \"\"
      End If
   End Function
从文件读取文本的方法
  Private Function ReadText(ByVal TextFilePath As String) As String
    Using ReadStream As FileStream = File.OpenRead(TextFilePath)
      Dim FileTextBuilder As New StringBuilder()
      Dim DataTransit As Byte() = New Byte(ReadStream.Length) {}
      Dim DataEncoding As New UTF8Encoding(True)
      While ReadStream.Read(DataTransit, 0, DataTransit.Length) > 0
          FileTextBuilder.Append(DataEncoding.GetString(DataTransit))
      End While
      Return FileTextBuilder.ToString()
    End Using
  End Function
    
已邀请:
您不能使用File.ReadAllText()方法读取整个文件,然后使用与文本框相同的方式解密吗? 我知道,如果文件很大,这不是一个好主意,但是您可以尝试一下,看文件是否保存正确,或者阅读是否错误。     

要回复问题请先登录注册