0

尝试对项目使用 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# 代码相同的结果?

4

1 回答 1

1

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

于 2012-04-18T12:11:48.553 回答