[RESOLVED] Decrypting (text)File Failure
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:
' encrypt the file
Private Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(strEncrKey)
Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
Dim des As New DESCryptoServiceProvider
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
cs.Write(InputByteArray, 0, InputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
' decrypt the file
Private Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(sDecrKey)
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
' read the file
If File.Exists(Application.StartupPath & "\myfile.file") Then
decryption_label.Text = Decrypt(Application.StartupPath & "\myfile.file", "passw")
Else
'...
Someone know what could be the issue? Thanks for the help in advance.
Re: Decrypting (text)File Failure
You are passsing the file path to decrypt method ..I think you should pass the file content as string to the method., not just the file name.
Re: Decrypting (text)File Failure
No, it should be possible, cause I have done it before with the same method and it worked. Just don;t get I why it doesn't work this time. Besides that I need to decrypt the file, can't hardcode it.
Re: Decrypting (text)File Failure
You want to decrypt the content of the file , right ? Whatever is written in the file ?
So I think you need to open the file and read the content first and then pass the entire encrypted string to Decrypt method.
Re: Decrypting (text)File Failure
You are correct. Stupid that I didn't think of that. :o
Re: Decrypting (text)File Failure
Thanks man. Really stupid of me. :p