memorystream
-
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.
-
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.
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 -
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, 2007Dave 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;
-
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;
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)
-
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)
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;
-
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;
thanks. I resolved the problem,now it works.