关于hmac md5算法的C#或vb.net的实现 小弟需要一份hmac md5的.net实现,有哪位哥们实现过,希望能给一份,先谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 .NET类库中就有啊在winform中需要引入:System.Web位于:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Web.dll下面是实现using System.Web.Security;string md5=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"md5"); System.SecuritySystem.Web.Security这两个命名空间下有关于加密的内容,请参阅MSDN 不用.netframework类的c# md5的算法实现也有啊,我记得在blog里面看过,自己去搜索一下嘛。 是hmac md5不是单纯的md5,要能带密钥的,c代码如下,我需要一份c#的/*** Function: hmac_md5*/voidhmac_md5(text, text_len, key, key_len, digest)unsigned char* text; /* pointer to data stream */int text_len; /* length of data stream */unsigned char* key; /* pointer to authentication key */int key_len; /* length of authentication key */caddr_t digest; /* caller digest to be filled in */{ MD5_CTX context; unsigned char k_ipad[65]; /* inner padding - * key XORd with ipad */ unsigned char k_opad[65]; /* outer padding - * key XORd with opad */ unsigned char tk[16]; int i; /* if key is longer than 64 bytes reset it to key=MD5(key) */ if (key_len > 64) { MD5_CTX tctx; MD5Init(&tctx); MD5Update(&tctx, key, key_len); MD5Final(tk, &tctx); key = tk; key_len = 16; } /* * the HMAC_MD5 transform looks like: * * MD5(K XOR opad, MD5(K XOR ipad, text)) * * where K is an n byte key * ipad is the byte 0x36 repeated 64 times * opad is the byte 0x5c repeated 64 times * and text is the data being protected */ /* start out by storing key in pads */ bzero( k_ipad, sizeof k_ipad); bzero( k_opad, sizeof k_opad); bcopy( key, k_ipad, key_len); bcopy( key, k_opad, key_len); /* XOR key with ipad and opad values */ for (i=0; i<64; i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } /* * perform inner MD5 */ MD5Init(&context); /* init context for 1st * pass */ MD5Update(&context, k_ipad, 64) /* start with inner pad */ MD5Update(&context, text, text_len); /* then text of datagram */ MD5Final(digest, &context); /* finish up 1st pass */ /* * perform outer MD5 */ MD5Init(&context); /* init context for 2nd * pass */ MD5Update(&context, k_opad, 64); /* start with outer pad */ MD5Update(&context, digest, 16); /* then results of 1st * hash */ MD5Final(digest, &context); /* finish up 2nd pass */} /** * * hmac_md5口令加密算法 * */ public byte[] hmac_md5(string timespan, string password) { byte[] b_tmp; byte[] b_tmp1; if (password == null) { return null; } byte[] digest = new byte[512]; byte[] k_ipad = new byte[64]; byte[] k_opad = new byte[64]; byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(password); System.Security.Cryptography.MD5 shainner = new MD5CryptoServiceProvider(); for (int i = 0; i < 64; i++) { k_ipad[i] = 0 ^ 0x36; k_opad[i] = 0 ^ 0x5c; } try { if (source.Length > 64) { shainner = new MD5CryptoServiceProvider(); source = shainner.ComputeHash(source); } for (int i = 0; i < source.Length; i++) { k_ipad[i] = (byte) (source[i] ^ 0x36); k_opad[i] = (byte) (source[i] ^ 0x5c); } b_tmp1 = System.Text.ASCIIEncoding.ASCII.GetBytes(timespan); b_tmp = adding(k_ipad,b_tmp1); shainner = new MD5CryptoServiceProvider(); digest = shainner.ComputeHash(b_tmp); b_tmp = adding(k_opad,digest); shainner = new MD5CryptoServiceProvider(); digest = shainner.ComputeHash(b_tmp); return digest; } catch (Exception e) { throw e; } } /** * * 填充byte * */ public byte[] adding(byte[] a,byte[] b) { byte[] c = new byte[a.Length+b.Length]; a.CopyTo(c,0); b.CopyTo(c,a.Length); return c; } 新手,求助啊!Help,help! WCF身份认证 哪位大哥能推荐一个好的代码文档生成器 关于记事本代码 报表问题 我打开报表打印太慢~!! winform可以在运行的时候带参数执行吗? web发布打包问题 C# 哪个有没有C#.net的实例开发的书。 超男,曲线包含点的问题,见解有分!!!! 怎么样完成这个sql注入 急,急,急,一个简单的问题,帮我一下。
需要引入:System.Web
位于:
\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Web.dll下面是实现
using System.Web.Security;string md5=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"md5");
System.Web.Security
这两个命名空间下有关于加密的内容,请参阅MSDN
/*
** Function: hmac_md5
*/void
hmac_md5(text, text_len, key, key_len, digest)
unsigned char* text; /* pointer to data stream */
int text_len; /* length of data stream */
unsigned char* key; /* pointer to authentication key */
int key_len; /* length of authentication key */
caddr_t digest; /* caller digest to be filled in */{
MD5_CTX context;
unsigned char k_ipad[65]; /* inner padding -
* key XORd with ipad
*/
unsigned char k_opad[65]; /* outer padding -
* key XORd with opad
*/
unsigned char tk[16];
int i;
/* if key is longer than 64 bytes reset it to key=MD5(key) */
if (key_len > 64) { MD5_CTX tctx; MD5Init(&tctx);
MD5Update(&tctx, key, key_len);
MD5Final(tk, &tctx); key = tk;
key_len = 16;
} /*
* the HMAC_MD5 transform looks like:
*
* MD5(K XOR opad, MD5(K XOR ipad, text))
*
* where K is an n byte key
* ipad is the byte 0x36 repeated 64 times * opad is the byte 0x5c repeated 64 times
* and text is the data being protected
*/ /* start out by storing key in pads */
bzero( k_ipad, sizeof k_ipad);
bzero( k_opad, sizeof k_opad);
bcopy( key, k_ipad, key_len);
bcopy( key, k_opad, key_len); /* XOR key with ipad and opad values */
for (i=0; i<64; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/*
* perform inner MD5
*/
MD5Init(&context); /* init context for 1st
* pass */
MD5Update(&context, k_ipad, 64) /* start with inner pad */
MD5Update(&context, text, text_len); /* then text of datagram */
MD5Final(digest, &context); /* finish up 1st pass */
/*
* perform outer MD5
*/
MD5Init(&context); /* init context for 2nd
* pass */
MD5Update(&context, k_opad, 64); /* start with outer pad */
MD5Update(&context, digest, 16); /* then results of 1st
* hash */
MD5Final(digest, &context); /* finish up 2nd pass */
}
*
* hmac_md5口令加密算法
*
*/
public byte[] hmac_md5(string timespan, string password)
{
byte[] b_tmp;
byte[] b_tmp1;
if (password == null)
{
return null;
}
byte[] digest = new byte[512];
byte[] k_ipad = new byte[64];
byte[] k_opad = new byte[64];
byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(password);
System.Security.Cryptography.MD5 shainner = new MD5CryptoServiceProvider();
for (int i = 0; i < 64; i++)
{
k_ipad[i] = 0 ^ 0x36;
k_opad[i] = 0 ^ 0x5c;
} try
{
if (source.Length > 64)
{
shainner = new MD5CryptoServiceProvider();
source = shainner.ComputeHash(source);
}
for (int i = 0; i < source.Length; i++)
{
k_ipad[i] = (byte) (source[i] ^ 0x36);
k_opad[i] = (byte) (source[i] ^ 0x5c);
}
b_tmp1 = System.Text.ASCIIEncoding.ASCII.GetBytes(timespan);
b_tmp = adding(k_ipad,b_tmp1);
shainner = new MD5CryptoServiceProvider();
digest = shainner.ComputeHash(b_tmp);
b_tmp = adding(k_opad,digest);
shainner = new MD5CryptoServiceProvider();
digest = shainner.ComputeHash(b_tmp);
return digest;
}
catch (Exception e)
{
throw e;
}
} /**
*
* 填充byte
*
*/
public byte[] adding(byte[] a,byte[] b)
{
byte[] c = new byte[a.Length+b.Length];
a.CopyTo(c,0);
b.CopyTo(c,a.Length);
return c;
}