Home All Groups Group Topic Archive Search About

Bad Data error in DES encryption

Author
4 Aug 2005 5:09 AM
Samba
Hi,

I'm doing a symmetric encryption using DES provider. I've posted my VB.NET
code below:

    ' Encryption
    Dim encAlg As New DESCryptoServiceProvider
    Dim ms As New MemoryStream
    Dim cs As New CryptoStream(ms,
encAlg.CreateEncryptor(ASCIIEncoding.ASCII.GetBytes("MyKey123"),
ASCIIEncoding.ASCII.GetBytes("MyIV1234")), CryptoStreamMode.Write)
    cs.Write(ASCIIEncoding.ASCII.GetBytes("My Plain Text"), 0,
ASCIIEncoding.ASCII.GetBytes("My Plain Text").Length)
    Dim cipBytes() As Byte = ms.ToArray()
    Dim cipString As String = ASCIIEncoding.ASCII.GetString(cipBytes)
    cs.Close()
    ms.Close()


    ' Decryption
    Dim decAlg As New DESCryptoServiceProvider
    Dim encByte() As Byte = ASCIIEncoding.ASCII.GetBytes(cipString)
    Dim msDec As New MemoryStream(encByte)
    Dim csDec As New CryptoStream(msDec,
decAlg.CreateDecryptor(ASCIIEncoding.ASCII.GetBytes("MyKey123"),
ASCIIEncoding.ASCII.GetBytes("MyIV1234")), CryptoStreamMode.Read)
    Dim srcByte(ASCIIEncoding.ASCII.GetBytes(cipString).Length - 1) As Byte
    csDec.Read(srcByte, 0, encByte.Length)
    Dim decByte() As Byte = srcByte
    Dim decStr As String = ASCIIEncoding.ASCII.GetString(srcByte)
    msDec.Close()
    csDec.Close()

But, if I run the above code, I get a CryptographicException saying "Bad
Data". I tried my best but couldn't identify the cause.

Any help is highly appreciated. Thanks very much in advance.

--
Samba!

Author
4 Aug 2005 1:08 PM
Valery Pryamikov
Hi,
of course you get Bad Data error! :D
Cipher text is binary randomly looking data, and not "text". You can't use
ASCIIEncoding on cipher text.
You want to store it as "text" - encode it with Base64 (or similar).

-Valery.
http://www.harper.no/valery

Show quoteHide quote
"Samba" <sri***@dotnet.microsoft.com> wrote in message
news:DE2923E3-E472-4EDD-90FF-BE6488F44640@microsoft.com...
> Hi,
>
> I'm doing a symmetric encryption using DES provider. I've posted my VB.NET
> code below:
>
>    ' Encryption
>    Dim encAlg As New DESCryptoServiceProvider
>    Dim ms As New MemoryStream
>    Dim cs As New CryptoStream(ms,
> encAlg.CreateEncryptor(ASCIIEncoding.ASCII.GetBytes("MyKey123"),
> ASCIIEncoding.ASCII.GetBytes("MyIV1234")), CryptoStreamMode.Write)
>    cs.Write(ASCIIEncoding.ASCII.GetBytes("My Plain Text"), 0,
> ASCIIEncoding.ASCII.GetBytes("My Plain Text").Length)
>    Dim cipBytes() As Byte = ms.ToArray()
>    Dim cipString As String = ASCIIEncoding.ASCII.GetString(cipBytes)
>    cs.Close()
>    ms.Close()
>
>
>    ' Decryption
>    Dim decAlg As New DESCryptoServiceProvider
>    Dim encByte() As Byte = ASCIIEncoding.ASCII.GetBytes(cipString)
>    Dim msDec As New MemoryStream(encByte)
>    Dim csDec As New CryptoStream(msDec,
> decAlg.CreateDecryptor(ASCIIEncoding.ASCII.GetBytes("MyKey123"),
> ASCIIEncoding.ASCII.GetBytes("MyIV1234")), CryptoStreamMode.Read)
>    Dim srcByte(ASCIIEncoding.ASCII.GetBytes(cipString).Length - 1) As Byte
>    csDec.Read(srcByte, 0, encByte.Length)
>    Dim decByte() As Byte = srcByte
>    Dim decStr As String = ASCIIEncoding.ASCII.GetString(srcByte)
>    msDec.Close()
>    csDec.Close()
>
> But, if I run the above code, I get a CryptographicException saying "Bad
> Data". I tried my best but couldn't identify the cause.
>
> Any help is highly appreciated. Thanks very much in advance.
>
> --
> Samba!
>
Author
4 Aug 2005 3:04 PM
Samba
Oops, I dint know that. Thanks for the useful info.
By the way, where would I get such basic and other info about Cryptography.
I badly need this as I'm quite new to this. Thanks.
--
Samba!
Author
4 Aug 2005 3:40 PM
Joe Kaplan (MVP - ADSI)
This isn't really an issue particular to cryptography so much as just basic
understanding of how strings and encodings work.

The only type of byte array that you can convert to a string with ASCII
encoding is a byte array that contains an ASCII-encoded string.  You can't
safely take an arbitrary array of bytes and convert it to ANY kind of string
because there is no guarantee that it represents an encoding of any kind
string at all.  The normal safe conversions of arbitrary binary data to
strings are Base64 and hex string encoding.  Base64 is generally preferred
because it is more compact, but both get used.

If you have a string and want to convert it to binary data, then you can use
a string encoding for this.  However, it is dangerous to use ASCII even for
this purpose on a platform like .NET as all strings in .NET are unicode.  If
you string happens to contain unicode characters, converting to an ASCII
encoding will cause data loss.  It is generally always safer to use UTF8
encoding for string data as it handles unicode without data loss.  UTF8 has
the added benefit of being backward compatible with ASCII for ASCII
characters, so there is no size penalty for using it in that case.

The only time ASCII is safe is when you are positive you only have ASCII
characters involved or you are not concerned about data loss and simply want
an ASCII representation.  However, this is rarely the case for any crypto
tasks, so its use is discouraged.

HTH,

Joe K.
Show quoteHide quote
"Samba" <sri***@dotnet.microsoft.com> wrote in message
news:3D1C533D-5AAE-44AE-A126-81358B715555@microsoft.com...
> Oops, I dint know that. Thanks for the useful info.
> By the way, where would I get such basic and other info about
> Cryptography.
> I badly need this as I'm quite new to this. Thanks.
> --
> Samba!
>
Author
5 Aug 2005 6:07 AM
Samba
Thanks Joe for enlightening me ...
--
Samba!