Results 1 to 6 of 6

Thread: [RESOLVED] Decrypting (text)File Failure

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [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:
    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    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.
    thanks
    amrita

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    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.
    thanks
    amrita

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Decrypting (text)File Failure

    You are correct. Stupid that I didn't think of that.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Decrypting (text)File Failure

    Thanks man. Really stupid of me.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width