开发者

Invalid character in a Base-64 string when attempting decryption

开发者 https://www.devze.com 2023-02-25 02:50 出处:网络
I have a encryption/decryption method that works just fine with one exception. When I attempt to read in encrypted text from a text file and then decrypt it I get the following error.

I have a encryption/decryption method that works just fine with one exception. When I attempt to read in encrypted text from a text file and then decrypt it I get the following error.

Invalid character in a Base-64 string

The strange thing is if I just read the encrypted text into a textbox and then copy and pate it into another text box that decrypts used the same decryption method it works just fine. No errors and the decryption proceeds. I am listing the decryption method and method used to read in the text file below.

Decryption Method

   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

Method to read text from file

  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(Data开发者_开发百科Transit, 0, DataTransit.Length) > 0
          FileTextBuilder.Append(DataEncoding.GetString(DataTransit))
      End While
      Return FileTextBuilder.ToString()
    End Using
  End Function


Can't you use File.ReadAllText() method to read the whole file and then decrypt the same way you do with textboxes?
I know, if file is huge that's not a good idea, but you can give it a try to see if file is well saved or if you're reading it bad.

0

精彩评论

暂无评论...
验证码 换一张
取 消