Ola,

Trying to decrypt a textfile is giving me an error: The input is not a valid Base 64-string as it contains a non-Base 64 character, more then two padding characters, or a non-white space character among the padding characters.

Besides that I also get an exception while debugging: A first chance exception of type 'System.FormatException' occurred in Fenix.exe

When I start the app it opens the file and tries to decrypt it. After decryption the string is send to a label, so I can read it. The codes I used:

vb.net Code:
  1. '    encrypt the file
  2.     Private Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
  3.         Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
  4.         Try
  5.             Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(strEncrKey)
  6.             Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
  7.             Dim des As New DESCryptoServiceProvider
  8.             Dim ms As New MemoryStream
  9.             Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
  10.             cs.Write(InputByteArray, 0, InputByteArray.Length)
  11.             cs.FlushFinalBlock()
  12.             Return Convert.ToBase64String(ms.ToArray())
  13.         Catch ex As Exception
  14.             Return ex.Message
  15.         End Try
  16.     End Function
  17.  
  18.  
  19.     '    decrypt the file
  20.     Private Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
  21.         Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
  22.         Dim inputByteArray(strText.Length) As Byte
  23.         Try
  24.             Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(sDecrKey)
  25.             Dim des As New DESCryptoServiceProvider
  26.             inputByteArray = Convert.FromBase64String(strText)
  27.             Dim ms As New MemoryStream
  28.             Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
  29.             cs.Write(inputByteArray, 0, inputByteArray.Length)
  30.             cs.FlushFinalBlock()
  31.             Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
  32.             Return encoding.GetString(ms.ToArray())
  33.         Catch ex As Exception
  34.             Return ex.Message
  35.         End Try
  36.     End Function
  37.  
  38. '    read the file
  39.  
  40. If File.Exists(Application.StartupPath & "\myfile.file") Then
  41.     decryption_label.Text = Decrypt(Application.StartupPath & "\myfile.file", "passw")
  42. Else
  43. '...

Someone know what could be the issue? Thanks for the help in advance.