北森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();

        }
    }
}


 

转载于:https://www.cnblogs.com/xinbin/archive/2005/12/06/291528.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值