Re: DES Decrypt Not Working

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 05/27/04


Date: Thu, 27 May 2004 08:58:07 +0100

JustMe <andrewpery@hotmail.com> 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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: a problem with encryption
    ... >> That way I always get the original data I've encrypted. ... I really don't know a way to know how long the string I send to ... > encrypted data in a byte array trough network stream. ... to decrypt in one call, ...
    (microsoft.public.dotnet.general)
  • Re: RijndaelManaged decryption leaves strange characters
    ... When you convert the array of decrypted bytes to string, ... how many plain text bytes were returned, only convert these bytes to string. ... > Encrypt the data using RijndaelManaged in CBC Mode. ... > Decrypt using RijndaelManaged in CBC Mode ...
    (microsoft.public.dotnet.security)
  • Re: Java string encryption/decryption
    ... encrypted byte-array to a String before decrypting it. ... I'm trying to decrypt. ... The cyphertext will not make sense as a String and should not be ... Convert the plaintext to a byte array. ...
    (comp.lang.java.programmer)
  • problem storing encrypted string to database
    ... I need to encrypt a string, store it in a database varchar field, then ... I can encrypt to a byte array and then decrypt the byte array no ... //later I want to pull "EncryptedMsg" from the database ...
    (microsoft.public.dotnet.security)
  • Re: a problem with encryption
    ... I didn't say it *was* in the network operations. ... > byte array for example. ... Byte arrays don't have a DeCrypt ...
    (microsoft.public.dotnet.general)