Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FlushFinalBlock() method was called twice on a CryptoStream

270 views
Skip to first unread message

Khor Soon Hua

unread,
Jul 11, 2003, 7:14:04 AM7/11/03
to
when i run the following code, the above error is occured. can anybody
help me? sorry cosz it is a vb.net code.

Public Function Decrypt(ByVal strToDecrypt As String) As String

Dim sb As New StringBuilder
Dim byteToDecrypt() As Byte = Convert.FromBase64String(strToDecrypt)
Dim byteDecoded(byteToDecrypt.Length) As Byte
Dim ms As New MemoryStream(byteToDecrypt)
Dim cs As CryptoStream

Try
byteKey = GetKey(strKey)
byteIV = GetKey(strIV)
Dim desProvider As New DESCryptoServiceProvider
cs = New CryptoStream(ms, desProvider.CreateDecryptor(byteKey,
byteIV), CryptoStreamMode.Read)
cs.Read(byteDecoded, 0, byteDecoded.Length)
cs.FlushFinalBlock()
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
ms.Close()
cs.Close()
End Try

Dim intLength As Integer = byteDecoded.Length - 1
For i As Integer = 0 To intLength
sb.Append(byteDecoded(i).ToString)
Next
Return sb.ToString
End Function

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Rich Blum

unread,
Jul 11, 2003, 11:44:35 PM7/11/03
to
Khor Soon Hua <cefi...@hotmail.com> wrote in message news:<uYI1K25R...@TK2MSFTNGP11.phx.gbl>...

> when i run the following code, the above error is occured. can anybody
> help me? sorry cosz it is a vb.net code.
>
> cs.Read(byteDecoded, 0, byteDecoded.Length)
> cs.FlushFinalBlock()

Then why not post to the vb.net group? I think the problem is that
the FlushFinalBlock() method is used by the writer after data is
written to the stream, to ensure that the data buffer has been
properly flushed. It should not be used by the reader when data is
read from the stream. When you call it here, it has already been
called by whatever wrote the data to the stream, thus the error
message. Try removing the FlushFinalBlock() method, and the error
should go away.

Rich Blum
Author of "C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176

0 new messages