**
*
* hmac_md5口令加密算法
*
*/
public byte[] hmac_md5(string timespan, string password)//timespan 是随机
数,password 是密钥
{
byte[] b_tmp;
byte[] b_tmp1;
if (password == null)//密钥为空,不进行计算
{
return null;
}
byte[] digest = new byte[512];//摘要 digest,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; //54?
k_opad[i] = 0 ^ 0x5c; //92?
}
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;
}这是我看到的一个HMAC-MD5的程序,有几个地方不太明白:
1、 k_ipad[i] = 0 ^ 0x36;
k_opad[i] = 0 ^ 0x5c;
为什么要与0x36,0x5c进行^操作?而不是其他数?
2、byte[] digest = new byte[512];//摘要 digest,512位
byte[] k_ipad = new byte[64];
byte[] k_opad = new byte[64];
为什么要定义512,64位?
3、adding(byte[] a,byte[] b)函数的作用是什么?哪位高手能对上述程序给出比较详尽的注释?还有没有更简洁的程序?
*
* hmac_md5口令加密算法
*
*/
public byte[] hmac_md5(string timespan, string password)//timespan 是随机
数,password 是密钥
{
byte[] b_tmp;
byte[] b_tmp1;
if (password == null)//密钥为空,不进行计算
{
return null;
}
byte[] digest = new byte[512];//摘要 digest,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; //54?
k_opad[i] = 0 ^ 0x5c; //92?
}
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;
}这是我看到的一个HMAC-MD5的程序,有几个地方不太明白:
1、 k_ipad[i] = 0 ^ 0x36;
k_opad[i] = 0 ^ 0x5c;
为什么要与0x36,0x5c进行^操作?而不是其他数?
2、byte[] digest = new byte[512];//摘要 digest,512位
byte[] k_ipad = new byte[64];
byte[] k_opad = new byte[64];
为什么要定义512,64位?
3、adding(byte[] a,byte[] b)函数的作用是什么?哪位高手能对上述程序给出比较详尽的注释?还有没有更简洁的程序?
没人回答这个问题?