<%@ Page Language="C#" Description="MD5_HMAC ASP.NET" %> <%@ Import Namespace="System"%> <%@ Import Namespace="System.IO"%> <%@ Import Namespace="System.Security"%> <%@ Import Namespace="System.Security.Cryptography"%> <%@ Import Namespace="System.Text"%>
<script language="c#" runat=server> //by skyonline //Date: 2003/6/3 //MD5 Function 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; }
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; } </script>
<% //these for MD5_HMAC string ipad=""; string opad="";
{ for(int i=0; i<64; i++) { ipad += "6"; opad += "\\"; } } string Password= "Jefe"; int KLen = Password.Length; string iResult = ""; { for(int i = 0; i < 64; i++) { if(i < KLen) iResult += Convert.ToChar(ipad[i] ^ Password[i]); else iResult += Convert.ToChar(ipad[i]); } } iResult += "what do ya want for nothing?"; iResult = fun_MD5(iResult); byte[] Test = hexstr2array(iResult); iResult = ""; char[] b = System.Text.Encoding.GetEncoding(1252).GetChars(Test);
for(int i=0;i<b.Length;i++) { iResult += b[i]; }
string oResult = ""; { for (int i=0; i<64; i++) { if (i < KLen) oResult += Convert.ToChar(opad[i] ^ Password[i]); else oResult += Convert.ToChar(opad[i]); } }
oResult += iResult;
string Result = fun_MD5(oResult).ToUpper(); Message.Text += "<br> Congratulations! ::: "+Result; %> <html>
<body> <asp:label id="Message" forecolor="red" font-bold="true" runat=server/><br> </body> </html>
PS:希望对大家有帮助
如果要转载,请通知原作者:Email: zys_boy@163.net
//附:RFC2104
HMAC的定义。 定义HMAC需要一个加密用散列函数(表示为H)和一个密钥K。我们假设H是 一个将数据块用一个基本的迭代压缩函数来加密的散列函数。我们用B来表示数据块 的字长。(以上说提到的散列函数的分割数据块字长B=64),用L来表示散列函数的 输出数据字长(MD5中L=16,SHA—1中L=20)。鉴别密钥的长度可以是小于等于数 据块字长的任何正整数值。应用程序中使用的密钥长度若是比B大,则首先用使用散列 函数H作用于它,然后用H输出的L长度字符串作为在HMAC中实际使用的密钥。 一般情况下,推荐的最小密钥K长度是L个字长。(与H的输出数据长度相等)。更详 细的信息参见第三部分。 我们将定义两个固定且不同的字符串ipad,opad: (‘i'''',''''o''''标志内部与外部) ipad = the byte 0x36 repeated B times opad = the byte 0x5C repeated B times. 计算‘text''''的HMAC: H( K XOR opad, H(K XOR ipad, text)) 即为以下步骤:
(1) 在密钥K后面添加0来创建一个子长为B的字符串。(例如,如果K的字长是20 字节,B=60字节,则K后会加入44个零字节0x00)
(2) 将上一步生成的B字长的字符串与ipad做异或运算。
(3) 将数据流text填充至第二步的结果字符串中。
(4) 用H作用于第三步生成的数据流。
(5) 将第一步生成的B字长字符串与opad做异或运算。
(6) 再将第四步的结果填充进第五步的结果中。
(7) 用H作用于第六步生成的数据流,输出最终结果
[C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
|