北森hmac的C#实现
好久不用.net了,竟然生疏到了如此地步。幸亏辛苦一下午,终有成就。
原来提供的CSharp代码所用的Hmac库 竟然需要安装组件,极其郁闷,有了asp写hmac的经验,用net实现倒也容易(参考了网上的c#实现hmac),代码如下:
using System;
using System.Web;
using System.Data.OleDb;
namespace WebApplication1
{
/// <summary>
/// userBeisenTemp 的摘要说明。
/// </summary>
public class userBeisenTemp
{
private String key = "1234567890123456" ;
public userBeisenTemp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private String fun_MD5(string str)
{
byte[] b = System.Text.Encoding.GetEncoding(1252).GetBytes(str);
b=new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
string ret="";
for(int i=0;i<b.Length;i++)
ret+=b[i].ToString("x").PadLeft(2,'0');
return ret;
}
private Byte[] hexstr2array(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 = "";
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 hmac(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 = hexstr2array(k_ipad);
temp = "";
char[] b = System.Text.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();
}
}
}
using System.Web;
using System.Data.OleDb;
namespace WebApplication1
{
/// <summary>
/// userBeisenTemp 的摘要说明。
/// </summary>
public class userBeisenTemp
{
private String key = "1234567890123456" ;
public userBeisenTemp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private String fun_MD5(string str)
{
byte[] b = System.Text.Encoding.GetEncoding(1252).GetBytes(str);
b=new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
string ret="";
for(int i=0;i<b.Length;i++)
ret+=b[i].ToString("x").PadLeft(2,'0');
return ret;
}
private Byte[] hexstr2array(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 = "";
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 hmac(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 = hexstr2array(k_ipad);
temp = "";
char[] b = System.Text.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();
}
}
}