public bool hmac_md5(string timespan, string password, ref string outdigest)
{
byte[] b_tmp;
byte[] b_tmp1;
string szRet = string.Empty;
if (password == null || password.Length < 1)
{
return false;
}
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);
// for (int i = 0; i < digest.Length; i++)
// {
outdigest = System.Text.ASCIIEncoding.ASCII.GetString(digest);//[i].ToString();//
//
// }
return true;
}
catch (Exception e)
{
outdigest = e.Message.ToString();
return false;
}
}
/***
* * 填充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算法的具体实现过程,包括使用C#进行密码学操作的关键步骤,如密钥填充、消息摘要计算等,并展示了如何通过HMAC-MD5验证数据的完整性和真实性。

1935

被折叠的 条评论
为什么被折叠?



