62,073
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
string url = "/user/info";
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("appid", "801130847");
Params.Add("openid", "1DC79A6D95B2194B9F2969A35325696B");
Params.Add("openkey", "FF6571C24E75B949157FD0D0B7DA9268");
Params.Add("reqtime", "1348737913");
Params.Add("wbversion", "1");
string method = "get";
string secret = "b41d726150ced13fc3e7a3734e7d9d46";
string ret = GenerateSign(url, Params, method, secret);
Response.Write(ret);
}
/// <summary>
/// 生成签名
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <param name="method"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static string GenerateSign(string url, Dictionary<string, string> dirList, string method, string secret)
{
List<string> parameters = new List<string>();
foreach (string item in dirList.Keys)
{
parameters.Add(item + "=" + dirList[item]);
}
string[] sortParameters = BubbleSortASC(parameters.ToArray());
string tempParameters = string.Empty;
for (int i = 0; i < sortParameters.Length; i++)
{
if (tempParameters == string.Empty)
{
tempParameters = sortParameters[i];
}
else
{
tempParameters += "&" + sortParameters[i];
}
}
string source = method + "&" + UrlEncode(url) + "&" + UrlEncode(tempParameters);
//return hmacSha1(source, secret);
return HMACSHA1(source, secret);
}
public static string UrlEncode(string value)
{
value = HttpUtility.UrlEncode(value);
return System.Text.RegularExpressions.Regex.Replace(value, "(%[0-9a-f]{2})", c => c.Value.ToUpper());
}
/// <summary>
/// HMACSHA1加密
/// </summary>
/// <param name="content"></param>
/// <param name="key"></param>
/// <returns></returns>
private static string HMACSHA1(string content, string key)
{
HMACSHA1 hmac = new HMACSHA1(System.Text.ASCIIEncoding.ASCII.GetBytes(key));
CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
byte[] dataBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(content);
cs.Write(dataBytes, 0, dataBytes.Length);
cs.Close();
return Convert.ToBase64String(hmac.Hash, Base64FormattingOptions.None);
}
public static string hmacSha1(string encryptText, string encryptKey)
{
HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.Default.GetBytes(encryptKey));
byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.Default.GetBytes(encryptText));
return Convert.ToBase64String(RstRes);
}
/// <summary>
/// 排序算法
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
private static string[] BubbleSortASC(string[] r)
{
for (int i = 0; i < r.Length; i++)
{
bool flag = false;
for (int j = r.Length - 2; j >= i; j--)
{
if (string.CompareOrdinal(r[j + 1], r[j]) < 0)
{
string str = r[j + 1];
r[j + 1] = r[j];
r[j] = str;
flag = true;
}
}
if (!flag)
{
return r;
}
}
return r;
}
e3mhpyrx8bR+Y4g05WHrJC1EnzM=
SortedDictionary<string, string> sortedDic = new SortedDictionary<string, string>();
sortedDic.Add("appid", "801130847");
sortedDic.Add("openid", "1DC79A6D95B2194B9F2969A35325696B");
sortedDic.Add("openkey", "545221403EEA4385563647F3E4053D15");
sortedDic.Add("reqtime", "1351664179");
//sortedDic.Add("clienttip", "61.170.252.250");
sortedDic.Add("wbversion", "1");
string param = string.Empty;
foreach (KeyValuePair<string, string> keyValuePair in sortedDic)
{
param = param + keyValuePair.Key + "=" + keyValuePair.Value + "&";
}
string newParams = string.Format("{0}&{1}&{2}",
"GET"
, System.Web.HttpUtility.UrlEncode("/user/info")
, System.Web.HttpUtility.UrlEncode(param.TrimEnd('&')));
System.Security.Cryptography.HMACSHA1 hMACSHA1 = new System.Security.Cryptography.HMACSHA1();
hMACSHA1.Key = Encoding.UTF8.GetBytes("b41d726150ced13fc3e7a3734e7d9d46");
byte[] data = new byte[1024];
using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, hMACSHA1, System.Security.Cryptography.CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
}
byte[] reslut = hMACSHA1.Hash;
string base64 = Convert.ToBase64String(reslut);
这种 方式 就可以, 只是你的 上面的参数就一直在变, 你自己的 代码变了3次了都
//2•将除"sig"外的所有参数按key进行字典升序排列;
//2-1添加参数
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("appid", "801130847");
Params.Add("openid", "1DC79A6D95B2194B9F2969A35325696B");
Params.Add("openkey", "545221403EEA4385563647F3E4053D15");
Params.Add("clientip", GetClientIP());
Params.Add("reqtime", "1351574443");//a.ToString());
Params.Add("wbversion", "1");
Params.Add("format", "json");
//2-2排序
List<string> parameters = new List<string>();
foreach (string item in Params.Keys)
{
parameters.Add(item + "=" + Params[item]);
}
string[] sortParameters = BubbleSortASC(parameters.ToArray());
//排序
private static string[] BubbleSortASC(string[] r)
{
for (int i = 0; i < r.Length; i++)
{
bool flag = false;
for (int j = r.Length - 2; j >= i; j--)
{
if (string.CompareOrdinal(r[j + 1], r[j]) < 0)
{
string str = r[j + 1];
r[j + 1] = r[j];
r[j] = str;
flag = true;
}
}
if (!flag)
{
return r;
}
}
return r;
}