473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 1843
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.