首页 > 代码库 > 请教淘宝API sign签名问题,望高人指点!谢谢!

请教淘宝API sign签名问题,望高人指点!谢谢!

 


   对淘宝API sign签名老计算不对

代码如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //正确的sign
            //http://gw.api.taobao.com/router/rest?sign=70950D9D8BEC7BF55BA572902733F261×tamp=2014-05-28+00%3A04%3A02&v=2.0&app_key=21792839&method=taobao.time.get&partner_id=top-apitools&format=json
            Dictionary<string, string> param = new Dictionary<string, string>();

            param["app_key"] = "21792839";
            param["format"] = "json";
            param["method"] = "taobao.time.get";
            param["partner_id"] = "top-apitools";
            param["timestamp"] = "2014-05-28 00:04:02";
            param["v"] = "2.0";

            string secret = "c22cd49550a5fac8a775893a34a0391c";
            string x = getMD5String(param, secret);
            textBox1.Text = x;
        }
        public static String getMD5String(Dictionary<string, string> param, String str)
        {
            String gets = getBeforeSign(param, str);
            Console.WriteLine(gets);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash((new UTF8Encoding()).GetBytes(gets));
            //result = md5.ComputeHash(result);
            return byteArrayToHexString(result);
        }

        private static String byteArrayToHexString(byte[] b)
        {
            StringBuilder resultSb = new StringBuilder();
            string stmp;
            for (int i = 0; i < b.Length; i++)
            {
                int md = (b[i] & 0XFF);
                stmp = "" + md.ToString("X"); ;
                if (stmp.Length == 1)
                    resultSb.Append("0").Append(stmp);
                else
                    resultSb.Append(stmp);
            }
            return resultSb.ToString().ToUpper();
        }
        private static String getBeforeSign(Dictionary<String, String> param, String orgin)
        {
            String indexs = orgin;
            if (param == null)
                return null;
            foreach (KeyValuePair<String, String> var in param)
            {
                String name = (String)var.Key.ToString();
                orgin += name;
                orgin += param[name];
            }

            orgin = orgin + indexs;
            return orgin;
        }

    }
}


运行结果:

848457EDB79276ACDC1B1E636CDC5757

 

正确结果:

70950D9D8BEC7BF55BA572902733F261

请问是什么问题?