php代码在asp.net vb中等效

时间:2018-03-26 11:46:19

标签: php vb.net

我正在研究支付系统,php中的原始代码工作正常,现在我在asp.net(vb)上重写它,我一步一步地改变它并在这个阶段停止“php”之间的不同结果“.NET”。

PHP代码:

$sha1Signature = "484cea8e6bd1153674548ebab5a3673a5c3d0381";

$base64Sha1Signature = base64_encode(pack("H*",$sha1Signature));

echo $base64Sha1Signature ;

结果: SEzqjmvRFTZ0VI66taNnOlw9A4E =

.NET代码:

    Function Pack2(strToPack As String) As Byte()
        Dim raw_bytes As Byte() = New Byte(15) {}
        For i As Integer = 0 To 32 - 1 Step 2
            raw_bytes(i / 2) = Convert.ToByte(strToPack.Substring(i, 2), 16)
        Next

        Return raw_bytes
    End Function

    Function getBase64Code(strToCode As String, Optional pack As Boolean = False) As String
        Dim byt As Byte() = System.Text.Encoding.ASCII.GetBytes(strToCode)

        If pack Then
            byt = Pack2(strToCode)
        End If

        Return Convert.ToBase64String(byt)

    End Function

 Dim sha1Signature As String = "484cea8e6bd1153674548ebab5a3673a5c3d0381"

Response.Write(getBase64Code(sha1Signature, True))    

结果: SEzqjmvRFTZ0VI66taNnOg ==

如果您看到结果几乎相同且仅在最后4个字母中有所不同,这意味着我接近成功:)结果必须相同。

PHP:SEzqjmvRFTZ0VI66taNnOlw9A4E =

.NET:SEzqjmvRFTZ0VI66taNnOg ==

1 个答案:

答案 0 :(得分:1)

你的循环应该从0运行到“input-string-length”。此外,bytearray的init应该是十六进制字符串的一半。

    static void Main(string[] args)
    {
        string tmp = "484cea8e6bd1153674548ebab5a3673a5c3d0381";

        Console.WriteLine(Convert.ToBase64String(Pack2(tmp)));
    }

    public static byte[] Pack2(string hexString)
    {
        byte[] bytes = new byte[hexString.Length / 2]; // two hex-digits are one byte

        for (int i = 0; i < hexString.Length; i += 2)
            bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);

        return bytes;
    }