سؤال

Trying to use TripleDesCrypto encoding for a project and I keep getting a Bad Data error on decoding. This is what I had in VB.net

Public Shared Function encode(message As String) As String
    Dim _Key As Byte() = ASCIIEncoding.ASCII.GetBytes("asdf1325asdfs123")
    Dim _IV As Byte() = ASCIIEncoding.ASCII.GetBytes("123ads12")
    Dim sOutput As String = ""
    Try

        Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
        Dim InputBuffer As Byte() = Encoding.UTF8.GetBytes(message)
        Dim ms As New MemoryStream()
        Dim encStream As New CryptoStream(ms, tdes.CreateEncryptor(_Key, _IV), CryptoStreamMode.Write)
        encStream.Write(InputBuffer, 0, InputBuffer.Length)
        sOutput = Convert.ToBase64String(ms.ToArray())
        encStream.Close()
        ms.Close()
    Catch ex As Exception
        Throw New ArgumentException("Couldn't Encode Message: " + ex.Message)
    End Try
    Return sOutput
End Function

which returns

Cui4ahedjTI=

So I tried the same thing in C#.net with this

    public string encode(string message)
    {
        byte[] _Key  = ASCIIEncoding.ASCII.GetBytes("asdf1325asdfs123");
        byte[] _IV = ASCIIEncoding.ASCII.GetBytes("123ads12");
        string sOutput = "";
        try
        {
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            byte[] InputBuffer = Encoding.UTF8.GetBytes(message);
            MemoryStream ms = new MemoryStream();
            CryptoStream encStream = new CryptoStream(ms, tdes.CreateEncryptor(_Key, _IV), CryptoStreamMode.Write);
            encStream.Write(InputBuffer, 0, InputBuffer.Length);
            encStream.FlushFinalBlock();
            sOutput = Convert.ToBase64String(ms.ToArray());
            encStream.Close();
            ms.Close();
        }
        catch (Exception ex)
        {
            throw new ArgumentException("couldn't encode message: " + ex.Message);
        }

        return sOutput;
    }

which returns

ac6EeiwfAQHk26AhfAfaHA==

The decoding is occurs in a third party app which I'm assuming is written in C#

Question is why are the results different and is there some way to make the vb.net code return the same result as the C# code?

هل كانت مفيدة؟

المحلول

at first glance, I can see that the C# version has a call to encStream.FlushFinalBlock();, but the VB doesn't. Can't this be the difference?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top