4,250
社区成员




using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace eName.Payment
{
public class HMAC
{
private string Fun_MD5(string str)
{
byte[] b = Encoding.GetEncoding(1252).GetBytes(str);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = string.Empty;
foreach (int i in b)
{
ret += i.ToString("x").PadLeft(2, '0');
}
return ret;
}
private byte[] HexStringToArray(string HexStr)
{
string HEX = "0123456789ABCDEF";
string str = HexStr.ToUpper();
int len = str.Length;
byte[] retByte = new byte[len / 2];
for (int i = 0; i < len / 2; i++)
{
int NumHigh = HEX.IndexOf(str[i * 2]);
int NumLow = HEX.IndexOf(str[i * 2 + 1]);
retByte[i] = Convert.ToByte(NumHigh * 16 + NumLow);
}
return retByte;
}
private string StrXor(string password, string pad)
{
string iResult = string.Empty;
int KLen = password.Length;
for (int i = 0; i < 64; i++)
{
if (i < KLen)
iResult += Convert.ToChar(pad[i] ^ password[i]);
else
iResult += Convert.ToChar(pad[i]);
}
return iResult;
}
public string Maker(string data, string password)
{
string k_ipad, k_opad, temp;
string ipad = "6666666666666666666666666666666666666666666666666666666666666666";
string opad = @"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
k_ipad = Fun_MD5(StrXor(password, ipad) + data);
k_opad = StrXor(password, opad);
byte[] Test = HexStringToArray(k_ipad);
temp = string.Empty;
char[] b = Encoding.GetEncoding(1252).GetChars(Test);
for (int i = 0; i < b.Length; i++)
{
temp += b[i];
}
temp = k_opad + temp;
return Fun_MD5(temp).ToLower();
}
}
}