<%if(uKeyEnabled){%>
<OBJECT id="iKeyClient" data="data:application/x-oleobject;base64,fryh+TLN0xGNCgCgyZ/2KgADAADYEwAA2BMAAA=="
	classid="clsid:F9A1BC7E-CD32-11D3-8D0A-00A0C99FF62A" >
</OBJECT>
<SCRIPT language="JavaScript">
	var SerialNum, Challenge, Digest, ikeyDigest, strData, strGotoUrl;
	
	try
	{
		var url   =   window.location.href.split("//")[1].split("/")   
		strGotoUrl   =   url[url.length-1].split(".")[0] 
		
		try
		{
    		//OpenDevice(long lFlags, BSTR szAppId) 打开iKey设备
	
			document.iKeyClient.OpenDevice(1, "");
			
			// 其他打开方式
			// document.iKeyClient.OpenDevice(0x200,"11223344556677889900AABBCCDDEEFF");
			// document.iKeyClient.OpenDevice(0x100,"Ken.COM.Test")
					
			//document.write("OpenDevice succeeded.<br>");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "无法打开身份盾!";
			throw e;
		}        

	
		try
		{
			// get serial number
			SerialNum = document.iKeyClient.GetStringProperty(7, 0, "");
			
			//document.write("设备序列号: " + SerialNum + "<BR>");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "获取设备序列号出错!";
			throw e;
		} 
		
		try
		{
			//获取挑战随机数, 即服务器端产生的挑战数据
			Challenge = "<%=strChallenge%>";
	
			//document.write("Challenge: " + Challenge + "<BR>");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "Get challenge data failed!";
			throw e;
		}
		
		try
		{
			//change dir to 0x1000
			//document.iKeyClient.ChangeDir(0x200, 0x1000, "");
			document.iKeyClient.ChangeDir(0x200, 0x0001, "");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "Change dir failed!";
			throw e;
		}	
			
		try
		{    
			//哈希运算
			//OpenFile(long lFlags, long lFileId) 通过文件ID打开文件
			//CloseFile() 关闭上一次打开的文件
			//HashToken(long lFlags, long lRef, BSTR szBuffer) 通过打开的文件进行哈希运算
					
			//第一轮哈希运算        
			document.iKeyClient.OpenFile(0x40, 3);//打开第一个密钥文件 lFlags = IKEY_FILE_CRYPT, FileID = 3
			Digest = document.iKeyClient.HashToken(1, 0, Challenge);//第一轮哈希运算
			document.iKeyClient.CloseFile();//关闭文件
			
			//document.write("第一轮哈希运算结果: " + Digest + "<BR>");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "第一轮哈希运算结果 failed!";
			throw e;
		}
		try
		{
			//第二轮哈希运算        
			document.iKeyClient.OpenFile(0x40, 4);//打开第二个密钥文件 lFlags = IKEY_FILE_CRYPT, FileID = 4
			ikeyDigest = document.iKeyClient.HashToken(1, 0, Digest);//第二轮哈希运算
			document.iKeyClient.CloseFile();//关闭文件
			
			//document.write("第二轮哈希运算结果: " + ikeyDigest + "<BR>");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "第二轮哈希运算结果 failed!";
			throw e;
		}
			
		try
		{
			//读数据文件的内容
			document.iKeyClient.OpenFile(0x10, 2);//打开数据文件 lFlags = IKEY_FILE_READ, FileID = 2
			
			//Read(long lFlags, long lType, long lOffset, long lBytesToRead)
			// lFlags = 0; lType = IKEYS_FILE_ASCII （返回文本字符串）or IKEYS_FILE_BINARY（返回16进制字符串）;
			// lOffset 偏移; lBytesToRead 要读出的字节数
			// 更详细的内容见文档 
			
			strData = document.iKeyClient.Read(0, 0x00, 0, 15);//从0处读15字节
			
			document.iKeyClient.CloseFile();//关闭文件
			
			//document.write("数据文件内容: " + strData + "<BR>");
		}
		catch(e)
		{
			location.href = strGotoUrl + ".htm";
			e.description = "数据文件运算结果 failed!";
			throw e;
		}
					
		//关闭设备    
		document.iKeyClient.CloseDevice();
			
		//转到服务器端验证页面，如果您想查看客户端运行结果，可以将下面的语句注释
		//self.location = "iKeyServer.aspx?ikeyDigest=" + ikeyDigest + "&SerialNum=" + SerialNum + "&FileData=" + strData;
		
		content = "<%=strUKeyCode.Split('|')[0]%>";

		if(content == strData)
		{	
			//document.write( "身份验证通过");				
			//document.getElementById('divAdminLogin').style.display = "";
		}
		else
		{
			supercontent = "<%=strUKeyCode.Split('|')[1]%>";
			if(supercontent != strData)
			{
				location.href = strGotoUrl + ".htm";
				//return;
				document.write( "身份盾密码验证错误！");
			}
		}
	}
	catch(e)
	{
		location.href = strGotoUrl + ".htm";
		if(-2146827850 == e.number)
		{
			document.write( "不能正确加载iKey控件！");
		}
		else if("OpenDevice failed!" == e.description) 
		{
			document.write(e.description + "Error code: " + e.number);
		}
		else
		{
			//关闭设备    
			document.iKeyClient.CloseDevice();
			document.write(e.description + "Error code: " + e.number);
		}
	}
</SCRIPT>
<%}%>