PC Review


Reply
Thread Tools Rate Thread

DES Decrypt Not Working

 
 
JustMe
Guest
Posts: n/a
 
      26th May 2004
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
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th May 2004
JustMe <(E-Mail Removed)> wrote:
> 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?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
JustMe
Guest
Posts: n/a
 
      27th May 2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: decrypt problem: Length of the data to decrypt is invalid Oleg Subachev Microsoft C# .NET 4 21st Dec 2006 05:41 AM
Re: decrypt problem: Length of the data to decrypt is invalid Jon Skeet [C# MVP] Microsoft C# .NET 5 21st Dec 2006 01:12 AM
Re: decrypt problem: Length of the data to decrypt is invalid Robson Siqueira Microsoft C# .NET 1 19th Dec 2006 08:55 PM
"Length of the data to decrypt is invalid." when trying to decrypt TripleDes algorithm Dica Microsoft C# .NET 7 28th Oct 2005 02:07 PM
EFS can't decrypt Mike Wegner Windows XP Security 5 4th Feb 2004 04:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:35 PM.