memory Stream crypto bug ?

G

Guest

Hi,
Im reading a file in from disk as a byte array then passing it to a memory
stream for decryption using crypto api functions. What I have found is that
you need to
reduce the array length by 2 from the original lenght in order to get it to
work
as there seems to be 2 extra 0 bytes at the end.
Functions included

Stu

Public Function convFileToBinaryStream(ByVal p_cFileName As String) As
Byte()
Dim objFile As FileStream
Dim objBinaryStream As BinaryReader
Dim arByte() As Byte
Dim lnStreamCount, lnIndex As Integer
Dim lnStreamLen As Integer
Try

If File.Exists(p_cFileName) Then
objFile = File.Open(p_cFileName, FileMode.Open)

objBinaryStream = New BinaryReader(objFile)
lnStreamLen = CInt(objBinaryStream.BaseStream.Length)
ReDim arByte(lnStreamLen)
objBinaryStream.BaseStream.Read(arByte, 0, lnStreamLen)
Else
Throw New Exception("File to stream is not found")

End If

Catch ex As Exception
Throw New Exception(ex.Message)
Finally
objFile.Flush()
If Not IsNothing(objBinaryStream) Then
objBinaryStream.Close()
End If

If Not IsNothing(objFile) Then
objFile.Close()
End If
End Try
Return arByte
End Function

Public Function decryptByteStream() As Byte()
Dim bytKey(), arDecStream() As Byte
Dim lnIndex As Integer = -1
Dim lnValue As Integer = 0
Dim encrypto As ICryptoTransform
Dim cs As CryptoStream
Dim objMemStream As MemoryStream
Dim objBinReader As BinaryReader
Dim lnLength As Integer
Dim arBytes() As Byte
Try
bytKey = GetLegalKey(_cKey)

' some reason probably bug but memory stream need to be
exact size to work
' other wise invalid key error
ReDim arBytes(_arEncByteStream.Length - 2)
_arEncByteStream.Copy(_arEncByteStream, arBytes,
_arEncByteStream.Length - 1)

'set the private key
_CryptoService.Key = bytKey
_CryptoService.IV = bytIV

'create a Decryptor from the Provider Service instance
encrypto = _CryptoService.CreateDecryptor()

objMemStream = New MemoryStream(arBytes)

'create Crypto Stream that transforms a stream using the
decryption
cs = New CryptoStream(objMemStream, encrypto,
CryptoStreamMode.Read)

objBinReader = New BinaryReader(cs)
arDecStream = objBinReader.ReadBytes(objMemStream.Length)

Catch err As Exception
_cError = err.Message
Finally

If Not IsNothing(cs) Then
cs.Close()
End If
End Try

Return arDecStream
End Function
 
S

Stephen Martin

Your problem is with your array declarations. In VB when you declare an
array you are declaring the upper limit, not the size, of the array. In your
declaration: ReDim arByte(lnStreamLen) - you are declaring an array with
elements from 0 to lnStreamLen. This has a length of lnStreamLen + 1. In
your second procedure you then declare another array which was originally
(I'm guessing a bit here since it isn't entirely clear from the code): ReDim
arBytes(_arEncByteStream.Length) - which once again gives you an array with
a length one longer than you expect. So you've gone from a file of length
lnStreamLen to an array of lnStreamLen + 2 thus forcing you to subtract two
to get back to the original data. If you want an array of length lnStreamLen
in VB you should declare it: ReDim arByte(lnStreamLen - 1).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top