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
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