Home All Groups Group Topic Archive Search About

Extra '\0' chars after decryption

Author
5 Aug 2005 9:59 AM
Samba
Hi,
I'm trying encryption and decryption ... the problem is that whatever
symmetric algorithm I use, my decrypted string almost always contains a
couple of extra-spaces (in C#, I get two '\0' chars at the end of the
decrypted string). Otherwise, the string is correct. I couldn't find out if
I'm doing anything wrong in my code

My VB.NET code is given below:

        Dim encAlg As New TripleDESCryptoServiceProvider

        ' Encryption
        Dim msEnc As New MemoryStream
        Dim csEnc As New CryptoStream(msEnc,
encAlg.CreateEncryptor(ASCIIEncoding.ASCII.GetBytes(encKey),
ASCIIEncoding.ASCII.GetBytes(initVector)), CryptoStreamMode.Write)
        csEnc.Write(ASCIIEncoding.ASCII.GetBytes(txtLicenseCode.Text.Trim),
0, ASCIIEncoding.ASCII.GetBytes(txtLicenseCode.Text.Trim).Length)
        csEnc.FlushFinalBlock()
        csEnc.Flush()
        Dim cipBytes() As Byte = msEnc.ToArray()
        Dim cipString As String = Convert.ToBase64String(cipBytes, 0,
cipBytes.Length)
        csEnc.Close()
        msEnc.Close()


        ' Decryption
        Dim encByte() As Byte = Convert.FromBase64String(cipString)
        Dim msDec As New MemoryStream(encByte)
        Dim csDec As New CryptoStream(msDec,
encAlg.CreateDecryptor(ASCIIEncoding.ASCII.GetBytes(encKey),
ASCIIEncoding.ASCII.GetBytes(initVector)), CryptoStreamMode.Read)
        Dim srcByte(encByte.Length) As Byte
        csDec.Read(srcByte, 0, encByte.Length)
        'Dim decByte() As Byte = srcByte
        Dim decStr As String = ASCIIEncoding.ASCII.GetString(srcByte, 0,
srcByte.Length)
        MessageBox.Show(decStr.Trim & "A")
        msDec.Close()
        csDec.Close()

Thanks in advance for any help.

Even the Trim() method doesn't work (on the string after decryption) and
concatenation is just ignored (in my MessageBox.Show method)
--
Samba!

Author
5 Aug 2005 2:31 PM
Joe Kaplan (MVP - ADSI)
I use a function like this for doing both the encryption and decryption and
it works correctly:

Private Shared Function CryptTrans( _
    ByVal trans As ICryptoTransform, _
    ByVal data() As Byte _
    ) As Byte()

    Dim ms As New System.IO.MemoryStream
    Dim cs As New CryptoStream(ms, trans, CryptoStreamMode.Write)
    cs.Write(data, 0, data.Length)
    cs.FlushFinalBlock()
    Return ms.ToArray()
End Function

The idea is that you just pass in either the plain data (for encryption) or
the encrypted data (for decryption) and a properly initialized
ICryptoTransform that you got from your CreateDecryptor or CreateEncryptor
call.

Joe K.

Show quoteHide quote
"Samba" <sri***@dotnet.microsoft.com> wrote in message
news:43CE78D2-B4F9-4021-AE82-1E7D4B1D6B1B@microsoft.com...
> Hi,
> I'm trying encryption and decryption ... the problem is that whatever
> symmetric algorithm I use, my decrypted string almost always contains a
> couple of extra-spaces (in C#, I get two '\0' chars at the end of the
> decrypted string). Otherwise, the string is correct. I couldn't find out
> if
> I'm doing anything wrong in my code
>
> My VB.NET code is given below:
>
>        Dim encAlg As New TripleDESCryptoServiceProvider
>
>        ' Encryption
>        Dim msEnc As New MemoryStream
>        Dim csEnc As New CryptoStream(msEnc,
> encAlg.CreateEncryptor(ASCIIEncoding.ASCII.GetBytes(encKey),
> ASCIIEncoding.ASCII.GetBytes(initVector)), CryptoStreamMode.Write)
>        csEnc.Write(ASCIIEncoding.ASCII.GetBytes(txtLicenseCode.Text.Trim),
> 0, ASCIIEncoding.ASCII.GetBytes(txtLicenseCode.Text.Trim).Length)
>        csEnc.FlushFinalBlock()
>        csEnc.Flush()
>        Dim cipBytes() As Byte = msEnc.ToArray()
>        Dim cipString As String = Convert.ToBase64String(cipBytes, 0,
> cipBytes.Length)
>        csEnc.Close()
>        msEnc.Close()
>
>
>        ' Decryption
>        Dim encByte() As Byte = Convert.FromBase64String(cipString)
>        Dim msDec As New MemoryStream(encByte)
>        Dim csDec As New CryptoStream(msDec,
> encAlg.CreateDecryptor(ASCIIEncoding.ASCII.GetBytes(encKey),
> ASCIIEncoding.ASCII.GetBytes(initVector)), CryptoStreamMode.Read)
>        Dim srcByte(encByte.Length) As Byte
>        csDec.Read(srcByte, 0, encByte.Length)
>        'Dim decByte() As Byte = srcByte
>        Dim decStr As String = ASCIIEncoding.ASCII.GetString(srcByte, 0,
> srcByte.Length)
>        MessageBox.Show(decStr.Trim & "A")
>        msDec.Close()
>        csDec.Close()
>
> Thanks in advance for any help.
>
> Even the Trim() method doesn't work (on the string after decryption) and
> concatenation is just ignored (in my MessageBox.Show method)
> --
> Samba!
>