Results 1 to 2 of 2

Thread: Reading/Writing bytes help.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Question Reading/Writing bytes help.

    Hello...

    I'm trying to read 5 bytes from a file and then do something according to what has been read. This is my code (I'll explain in a bit what's wrong):

    vb.net Code:
    1. cStream = New CryptoStream(input, _
    2.                            RijndaelAlg.CreateDecryptor(_key, IV), _
    3.                            CryptoStreamMode.Read)
    4.  
    5.                     Dim HeaderBuffer(4) As Byte
    6.                     cStream.Read(HeaderBuffer, 0, 5)
    7.  
    8.                     If encoding.GetString(HeaderBuffer) <> HeaderString Then
    9.                         RaiseEvent ReturnType(ActionReturnType.InvalidPassword)
    10.                         cStream.Clear()
    11.                         Exit Try
    12.                     End If
    13.  
    14.                     Do While BytesProcessed < SourceLength
    15.                         CurrentBytes = cStream.Read(Buffer, 0, 4096)
    16.                         output.Write(Buffer, 0, CurrentBytes)
    17.                         BytesProcessed += CLng(CurrentBytes)
    18.                         PercentDone = CInt((BytesProcessed / SourceLength) * 100)
    19.                         RaiseEvent Progress(PercentDone)
    20.                     Loop

    It reads the 5 bytes I added to the encrypted file, but I want the CryptoStream to start from the sixth value and then copy the original bytes into the decrypted file, but it gives me an error:

    Padding is invalid and cannot be removed.

    That's why I'm trying to make it read from the sixth value and then continue copying normally, but I'm stuck.

    Any help will be greatly appreciated!

    Thanks .
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Reading/Writing bytes help.

    I have tested it and have taken a better look at the code. It reads the header and then, when inside the loop, it starts reading at the sixth byte. The problem that I've noticed is that the loop exits when the file reaches 99.90&#37; of its completion, why is this happening?

    This is my whole code... In case it helps.

    vb.net Code:
    1. Public Shared Function RijndaelEncryptDecrypt(ByVal source As String, ByVal destination As String, _
    2.                                                 ByVal key As String, _
    3.                                                 ByVal action As RijndaelCryptographicAction) _
    4.                                                 As Boolean
    5.         RijndaelAlg.BlockSize = 256
    6.  
    7.         GenerateOutput(source, destination, action)
    8.         Dim input As New FileStream(source, FileMode.Open, FileAccess.Read)
    9.         Dim output As New FileStream(destination, FileMode.OpenOrCreate)
    10.         Dim cStream As CryptoStream = Nothing
    11.  
    12.         Dim Buffer(4096) As Byte
    13.         Dim CurrentBytes As Integer = 0
    14.         Dim BytesProcessed As Long = 0
    15.         Dim SourceLength As Long = input.Length
    16.         Dim PercentDone As Integer = 0
    17.  
    18.         Dim _key As Byte() = CheckKey(key)
    19.         Dim IV As Byte() = GenerateIV(key)
    20.  
    21.         Try
    22.             Select Case action
    23.                 Case RijndaelCryptographicAction.Encrypt
    24.                     If source.EndsWith(".encx") Then
    25.                         output.Close()
    26.                         input.Close()
    27.  
    28.                         File.Delete(destination)
    29.  
    30.                         RaiseEvent ReturnType(ActionReturnType.EncryptInvalidFileType)
    31.                         Exit Try
    32.                     End If
    33.  
    34.                     cStream = New CryptoStream(output, _
    35.                                                RijndaelAlg.CreateEncryptor(_key, IV), _
    36.                                                CryptoStreamMode.Write)
    37.  
    38.                     cStream.Write(HeaderBytes, 0, 5)
    39.  
    40.                     Do While BytesProcessed < SourceLength
    41.                         CurrentBytes = input.Read(Buffer, 0, 4096)
    42.                         cStream.Write(Buffer, 0, CurrentBytes)
    43.                         BytesProcessed += CLng(CurrentBytes)
    44.                         PercentDone = CInt((BytesProcessed / SourceLength) * 100)
    45.                         RaiseEvent Progress(PercentDone)
    46.                     Loop
    47.  
    48.                 Case RijndaelCryptographicAction.Decrypt
    49.                     If Not source.EndsWith(".encx") Then
    50.                         output.Close()
    51.                         input.Close()
    52.  
    53.                         File.Delete(destination)
    54.  
    55.                         RaiseEvent ReturnType(ActionReturnType.DecryptInvalidFileType)
    56.                         Exit Try
    57.                     End If
    58.  
    59.                     cStream = New CryptoStream(input, _
    60.                                                RijndaelAlg.CreateDecryptor(_key, IV), _
    61.                                                CryptoStreamMode.Read)
    62.  
    63.                     Dim HeaderBuffer(4) As Byte
    64.                     cStream.Read(HeaderBuffer, 0, 5)
    65.  
    66.                     If encoding.GetString(HeaderBuffer) <> HeaderString Then
    67.                         RaiseEvent ReturnType(ActionReturnType.InvalidPassword)
    68.                         cStream.Clear()
    69.                         Exit Try
    70.                     End If
    71.  
    72.                     Do While BytesProcessed < SourceLength
    73.                         CurrentBytes = cStream.Read(Buffer, 0, 4096)
    74.                         output.Write(Buffer, 0, CurrentBytes)
    75.                         BytesProcessed += CLng(CurrentBytes)
    76.                         PercentDone = CInt((BytesProcessed / SourceLength) * 100)
    77.                         RaiseEvent Progress(PercentDone)
    78.                     Loop
    79.  
    80.             End Select
    81.  
    82.  
    83.             output.Close()
    84.             input.Close()
    85.  
    86.             File.Delete(source)
    87.  
    88.             RaiseEvent ReturnType(ActionReturnType.Good)
    89.             Return True
    90.  
    91.         Catch ex As Exception
    92.  
    93.             output.Close()
    94.             input.Close()
    95.  
    96.             RaiseEvent ReturnType(ActionReturnType.Bad)
    97.             Return False
    98.         End Try
    99.  
    100.     End Function
    Last edited by tassa; Sep 7th, 2009 at 10:04 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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