DES Decrypt Not Working

J

JustMe

Hi folks,

My code (in VB.Net) will encrypt data fine (I guess...) but when I try
to decrypt it, it returns the exact same byte array that I passed to
*be* decrypted! Your advice would be most appreciated. My code is
pretty simple (too simple?)....

Function EncryptData(ByVal bData() As Byte) As Byte()
Dim eDES As New DESCryptoServiceProvider
Dim eMS As New MemoryStream(bData.Length)
Dim EncStr As New CryptoStream(eMS, _
eDES.CreateEncryptor(DESKey, DESiv),
CryptoStreamMode.Write)
EncStr.Write(bData, 0, bData.Length)
EncStr.FlushFinalBlock()
Dim bResult(eMS.Position) As Byte
eMS.Position = 0
eMS.Read(bResult, 0, bResult.Length)
EncStr.Close()
eMS.Close()
eDES.Clear()
Return bResult
End Function

Function DecryptData(ByVal bData() As Byte) As String
Dim DES As New DESCryptoServiceProvider
Dim MS As New MemoryStream(bData.Length)
Dim DecStr As New CryptoStream(MS, _
DES.CreateDecryptor(DESKey, DESiv),
CryptoStreamMode.Read)
MS.Write(bData, 0, bData.Length)
DecStr.FlushFinalBlock()
MS.Position = 0
Dim Ret As String = New StreamReader(MS).ReadToEnd
DecStr.Close()
MS.Close()
DES.Clear()
Return Ret
End Function
 
J

Jon Skeet [C# MVP]

JustMe said:
My code (in VB.Net) will encrypt data fine (I guess...) but when I try
to decrypt it, it returns the exact same byte array that I passed to
*be* decrypted! Your advice would be most appreciated. My code is
pretty simple (too simple?)....

Function EncryptData(ByVal bData() As Byte) As Byte()
Dim eDES As New DESCryptoServiceProvider
Dim eMS As New MemoryStream(bData.Length)
Dim EncStr As New CryptoStream(eMS, _
eDES.CreateEncryptor(DESKey, DESiv),
CryptoStreamMode.Write)
EncStr.Write(bData, 0, bData.Length)
EncStr.FlushFinalBlock()
Dim bResult(eMS.Position) As Byte
eMS.Position = 0
eMS.Read(bResult, 0, bResult.Length)
EncStr.Close()
eMS.Close()
eDES.Clear()
Return bResult
End Function

Function DecryptData(ByVal bData() As Byte) As String
Dim DES As New DESCryptoServiceProvider
Dim MS As New MemoryStream(bData.Length)
Dim DecStr As New CryptoStream(MS, _
DES.CreateDecryptor(DESKey, DESiv),
CryptoStreamMode.Read)
MS.Write(bData, 0, bData.Length)
DecStr.FlushFinalBlock()
MS.Position = 0
Dim Ret As String = New StreamReader(MS).ReadToEnd
DecStr.Close()
MS.Close()
DES.Clear()
Return Ret
End Function

Well, you're relying on Stream.Read returning all the bytes you
requested in one chunk, which is in general unsafe but should be okay
with a MemoryStream. The MemoryStream.ToArray method makes it easier to
get the data in a MemoryStream, to be honest.

I note that you're decrypting to a string though, having *encrypted* a
byte array. This could well be part of the problem - was the data to be
encrypted a UTF-8 encoded version of a string?
 
J

JustMe

I've sorted out the problem. This is what the code should look like:

** mMemStr is a global memorystream **

Function EncryptData(ByVal sData As String) As String
Dim eDES As New TripleDESCryptoServiceProvider
mMemStr = New MemoryStream
Dim EncStr As New CryptoStream(mMemStr, _
eDES.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
Dim mStrWri As New StreamWriter(EncStr)
mStrWri.Write(sData)
mStrWri.Flush()
EncStr.FlushFinalBlock()
Dim mBytes(mMemStr.Length - 1) As Byte
mMemStr.Position = 0
mMemStr.Read(mBytes, 0, mMemStr.Length)
Dim mEnc As New UTF8Encoding
Return = mEnc.GetString(mBytes)
End Function

Function DecryptData() As String
Dim DES As New TripleDESCryptoServiceProvider
mMemStr.Position = 0
Dim DecStr As New CryptoStream(mMemStr, _
DES.CreateDecryptor(Key, IV), CryptoStreamMode.Read)
Dim mStrRead As New StreamReader(DecStr)
Dim sRet as String
Try
sRet = mStrRead.ReadToEnd()
Catch ee As CryptographicException
MsgBox("Exception : " & ee.Message)
Exit Function
End Try
DecStr.Close()
mStrRead.Close()
mMemStr.Close()
Return sRet
End Function
 

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