C#TripleDesCrypto提供的结果与VB TripleDesCrypto不同

时间:2012-04-17 21:29:29

标签: vb.net cryptography 3des

尝试对项目使用TripleDesCrypto编码,并且在解码时不断收到错误数据错误。这就是我在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

返回

Cui4ahedjTI=

所以我用C#.net尝试了同样的事情

    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;
    }

返回

ac6EeiwfAQHk26AhfAfaHA==

解码发生在第三方应用程序中,我假设是用C#编写的

问题是为什么结果不同,是否有某种方法使vb.net代码返回与C#代码相同的结果?

1 个答案:

答案 0 :(得分:1)

乍一看,我可以看到C#版本调用encStream.FlushFinalBlock();,但VB没有。这不是区别吗?