Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. memorystream

memorystream

Scheduled Pinned Locked Moved Visual Basic
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WhiteGirl23
    wrote on last edited by
    #1

    Hi, I have a problem with the memorystream. I read a file in memorystream . The length of the file is 178 and the length of the memorystream is 160. And when I convert the bytes from memorystream in string I see that the file is not complete. Can I lose bytes or the memorystream doesn't hold all the information? Please help me because I don't have a clue. Thanks.

    D 1 Reply Last reply
    0
    • W WhiteGirl23

      Hi, I have a problem with the memorystream. I read a file in memorystream . The length of the file is 178 and the length of the memorystream is 160. And when I convert the bytes from memorystream in string I see that the file is not complete. Can I lose bytes or the memorystream doesn't hold all the information? Please help me because I don't have a clue. Thanks.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Standard question #2. What does the code look like that you're using to get the file into the memory stream?

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      G 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Standard question #2. What does the code look like that you're using to get the file into the memory stream?

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Dave Kreskowiak wrote:

        Standard question #2. What does the code look like that you're using to get the file into the memory stream?

        No. Standard question #2 is: What error message do you get? You want standard question #3: What does your code look like? :)

        --- single minded; short sighted; long gone;

        W 1 Reply Last reply
        0
        • G Guffa

          Dave Kreskowiak wrote:

          Standard question #2. What does the code look like that you're using to get the file into the memory stream?

          No. Standard question #2 is: What error message do you get? You want standard question #3: What does your code look like? :)

          --- single minded; short sighted; long gone;

          W Offline
          W Offline
          WhiteGirl23
          wrote on last edited by
          #4

          my code is: Private Function DecryptFile(ByVal strInputFile As String, _ ByVal bytKey() As Byte, _ ByVal bytIV() As Byte) As String Dim strBuilder As New StringBuilder Dim mem As MemoryStream = New MemoryStream() 'Setup file streams to handle input and output. fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _FileAccess.Read) mem.SetLength(0) 'Declare variables for decrypt process. Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing Dim lngBytesProcessed As Long = 0 'running count of bytes processed Dim lngFileLength As Long = fsInput.Length 'the input file's length Dim intBytesInCurrentBlock As Integer 'current bytes being processed Dim csCryptoStream As CryptoStream 'Declare your CryptoServiceProvider. Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged csCryptoStream = New CryptoStream(mem, _ cspRijndael.CreateDecryptor(bytKey, bytIV), _ CryptoStreamMode.Write) 'Use While to loop until all of the file is processed. While lngBytesProcessed < lngFileLength 'Read file with the input filestream. intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096) 'Write output file with the cryptostream. csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock) 'Update lngBytesProcessed lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock) End While Dim result As Byte() = mem.ToArray() Dim valu As String = System.Text.Encoding.ASCII.GetString(result)

          G 1 Reply Last reply
          0
          • W WhiteGirl23

            my code is: Private Function DecryptFile(ByVal strInputFile As String, _ ByVal bytKey() As Byte, _ ByVal bytIV() As Byte) As String Dim strBuilder As New StringBuilder Dim mem As MemoryStream = New MemoryStream() 'Setup file streams to handle input and output. fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _FileAccess.Read) mem.SetLength(0) 'Declare variables for decrypt process. Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing Dim lngBytesProcessed As Long = 0 'running count of bytes processed Dim lngFileLength As Long = fsInput.Length 'the input file's length Dim intBytesInCurrentBlock As Integer 'current bytes being processed Dim csCryptoStream As CryptoStream 'Declare your CryptoServiceProvider. Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged csCryptoStream = New CryptoStream(mem, _ cspRijndael.CreateDecryptor(bytKey, bytIV), _ CryptoStreamMode.Write) 'Use While to loop until all of the file is processed. While lngBytesProcessed < lngFileLength 'Read file with the input filestream. intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096) 'Write output file with the cryptostream. csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock) 'Update lngBytesProcessed lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock) End While Dim result As Byte() = mem.ToArray() Dim valu As String = System.Text.Encoding.ASCII.GetString(result)

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            You forgot to close the cryptostream so that it gets flushed. The stream is encrypted in blocks of a set size (probably 32 bytes in this case), which means that you only get the completed blocks if you don't close the stream.

            --- single minded; short sighted; long gone;

            W 1 Reply Last reply
            0
            • G Guffa

              You forgot to close the cryptostream so that it gets flushed. The stream is encrypted in blocks of a set size (probably 32 bytes in this case), which means that you only get the completed blocks if you don't close the stream.

              --- single minded; short sighted; long gone;

              W Offline
              W Offline
              WhiteGirl23
              wrote on last edited by
              #6

              thanks. I resolved the problem,now it works.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups