473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

memory Stream crypto bug ?

Stu
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
Jul 21 '05 #1
1 1849
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).


"Stu" <St*@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.com...
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

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3754
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
16
3456
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
3
4578
by: JW | last post by:
I can encrypt the contents of a memory stream with no problems, however when decrypting( I'm using the same key an IV ) I get an exception stating bad data. I'm at a lost. I'm actually creating...
5
7780
by: Tomaz Koritnik | last post by:
Hi I have many short HTML files stored in a binary stream storage to display descriptions for various items in application. HTML would be display inside application using some .NET control or...
5
8030
by: Lee Gillie | last post by:
I am using Cryptography. You can encrypt or decrypt by providing an output stream as a parameter to the CryptoStream constructor. But I need byte arrays, as I am encrypting on the fly to a socket,...
2
323
by: Stu | last post by:
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...
0
1281
by: phreak008 | last post by:
I'm using SendMessage(...) to send a message to all other process that might run. It works well. My problem is when I try to pass data using shared memory. Here is my code. In the 1st process, I...
3
2930
by: CSharper | last post by:
I have a resource file that i open to write.After writing almost 300MB I call the close method and It failed with out of memory. One thing I learned that, in C#, resource writers doesn't write data...
12
2227
by: Fett | last post by:
I need a crypto package that works on windows with python 2.5. Can anyone suggest one for me? I have been searching for a couple days for a good cryptography package to use for public/private...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.