Decrypting Memory Streams

G

Guest

Hi,

Using the system.security.cryptography i have an encrypted byte() array
which Im trying to decrypt but it doesnt appear to work. It seams when I pass
the memory stream to the crypto stream nothing happens. Code below...

Any ideas TIA

Stu

Public Function decryptByteStream() As Byte()
Dim bytKey(), arStream(), arDecStream() As Byte
Dim encrypto As ICryptoTransform
Dim cs As CryptoStream
Dim objMemStream As MemoryStream
Dim objBinReader As BinaryReader
Try
bytKey = GetLegalKey(_cKey)

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

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

objMemStream = New MemoryStream(_arEncByteStream)

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

objBinReader = New BinaryReader(cs)
arDecStream = objBinReader.ReadBytes(cs.Length)
Catch err As Exception
_cError = err.Message
Finally
cs.Close()
End Try

Return arDecStream
End Function
 
G

Guest

Stu,
Don't use cs.FlushFinalBlock() which is usually used for
CryptoStreamMode.Write.

Rulin
 

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