/// <summary> /// 硬件加密狗计算HMAC_MD5 /// </summary> /// <param name="MD5KeyIndexInDog">密钥index[范围1~8,对应加密狗中密钥存储范围1~8]</param> /// <param name="origin">原始字串</param> /// <returns>加密后字串</returns> public static string HMAC_MD5_dog(int MD5KeyIndexInDog, string origin) { uint result; byte[] bytRandomCode = new byte[origin.Length]; //第四个参数为随机数 bytRandomCode = System.Text.Encoding.ASCII.GetBytes(origin); byte[] bytDigest = new byte[16]; //第五个参数为硬件中计算结果 //硬件中计算 //第一个参数为设备的handle句柄 //第二个参数为硬件中密钥索引 //第三个参数为随机数长度 //第四个参数为随机数 //第五个参数为硬件中计算结果 result = ET99_API.et_HMAC_MD5(ET99_API.dogHandle, MD5KeyIndexInDog, origin.Length, bytRandomCode, bytDigest); if (result == ET99_API.ET_SUCCESS) { //string strSoftDigest = string.Empty; //for (int i = 0; i < 16; i++) // strSoftDigest += string.Format("{0:X2}", bytDigest[i]); //return strSoftDigest; return(System.Text.Encoding.Default.GetString(bytDigest)); } else//失败 { return(string.Empty); } }
/// <summary> /// 闪烁LED灯n次 /// </summary> /// <param name="times">闪烁次数</param> /// <param name="interval">间隔毫秒数,300貌似不错</param> public static void FlashLED(int times, int interval) { int count = 0; System.Timers.Timer turnOffTimer = new System.Timers.Timer(interval); turnOffTimer.AutoReset = false; System.Timers.Timer turnOnTimer = new System.Timers.Timer(interval); turnOnTimer.AutoReset = false; turnOffTimer.Elapsed += new System.Timers.ElapsedEventHandler((m, n) => { ET99_API.et_TurnOffLED(ET99_API.dogHandle); turnOnTimer.Start(); }); turnOnTimer.Elapsed += new System.Timers.ElapsedEventHandler((m, n) => { count++; ET99_API.et_TurnOnLED(ET99_API.dogHandle); if (count < times) { turnOffTimer.Start(); } }); turnOffTimer.Start(); }
/// <summary> /// 打开并以用户权限进入加密狗 /// </summary> /// <param name="errMsg"></param> public static bool OpenDog(out string errMsg) { errMsg = string.Empty; int index = 1;//默认仅打开第一个加密狗 byte[] bytPID = new byte[8]; bytPID = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.PID); uint result = ET99_API.et_OpenToken(ref ET99_API.dogHandle, bytPID, index); if (result == ET99_API.ET_SUCCESS)//打开成功 { byte[] bytPIN = new byte[16]; bytPIN = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.UserPIN); result = ET99_API.et_Verify(ET99_API.dogHandle, ET99_API.ET_VERIFY_USERPIN, bytPIN); if (result == ET99_API.ET_SUCCESS) { return(true); } else { errMsg = string.Format("加密狗认证失败,请检查!\r\n错误:{0}", ET99_API.ShowResultText(result)); return(false); } } else { errMsg = string.Format("打开加密狗失败,请检查!\r\n错误:{0}", ET99_API.ShowResultText(result)); return(false); } }
/// <summary> /// 关闭加密狗 /// </summary> /// <param name="errMsg"></param> /// <returns></returns> public static bool CloseDog(out string errMsg) { errMsg = string.Empty; uint result = ET99_API.et_CloseToken(ET99_API.dogHandle); if (result == ET99_API.ET_SUCCESS) { ET99_API.dogHandle = System.IntPtr.Zero; return(true); } else { errMsg = string.Format("关闭加密狗失败,请检查!\r\n错误:{0}", ET99_API.ShowResultText(result)); return(false); } }
/// <summary> /// 读取指定地址数据区的字符串,并解密。 /// </summary> /// <param name="offset">偏移地址,范围0~999,字节为单位(整个数据区1000字节,每次读写限制长度60字节)</param> /// <param name="length">欲读取的字节长度</param> /// <returns></returns> public static string ReadOffsetDataAndDecrypt(int offset, int length) { string str = string.Empty; byte[] zyn = new byte[length]; uint resultmess; while (length > 60) { byte[] temp = new byte[60]; resultmess = ET99_API.et_Read(ET99_API.dogHandle, (ushort)offset, 60, temp);//读取60字节 if (resultmess != ET99_API.ET_SUCCESS) { ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("加密狗数据错误!", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); System.Windows.Forms.Application.Exit(); } Array.Copy(temp, 0, zyn, zyn.Length - length, 60); offset += 60; length -= 60; } //剩余数据,不需分割 byte[] others = new byte[length]; resultmess = ET99_API.et_Read(ET99_API.dogHandle, (ushort)offset, length, others);//读取 if (resultmess != ET99_API.ET_SUCCESS) { ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("加密狗数据错误!", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); System.Windows.Forms.Application.Exit(); } Array.Copy(others, 0, zyn, zyn.Length - length, length); //转化成字符串 str = System.Text.Encoding.Default.GetString(zyn); if (string.IsNullOrWhiteSpace(str.Replace('\0', ' '))) { return(string.Empty);//类似加密狗初期没有的数据‘起始时间’等,直接返回空。 } //======解密 int intRandom = new Random().Next(1, 9);//随即取1~8 string key16 = HMAC_MD5_dog(intRandom, "武汉创方科技"); return(RC2Decrypt(str, key16)); }
/// <summary> /// 查询本机是否安装加密狗 /// </summary> /// <param name="errMsg">如未检测到,回传错误信息</param> /// <returns></returns> public static bool FindDog(out string errMsg) { errMsg = string.Empty; byte[] bytPID = new byte[8]; int count = 0; bytPID = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.PID); uint result = ET99_API.et_FindToken(bytPID, out count); if (result == ET99_API.ET_SUCCESS) { return(true); } else { errMsg = string.Format("系统未检测到加密狗,请检查!\r\n错误:{0}", ET99_API.ShowResultText(result)); return(false); } }
/// <summary> /// 临时获得管理员权限,写入数据到加密狗,然后返回到用户权限 /// </summary> /// <param name="origin">欲写入的原始字串</param> /// <param name="offset">偏移字节地址</param> public static void TempAdminWriteDog(string origin, int offset) { //因为此函数的调用前提是通过校验了的狗,省略相关校验,加速 //获取SOPIN string sopin = Helper.ReadOffsetDataAndDecrypt(0, 32); //提升管理员权限 byte[] bytPIN = new byte[16]; bytPIN = System.Text.Encoding.ASCII.GetBytes(sopin); ET99_API.et_Verify(ET99_API.dogHandle, ET99_API.ET_VERIFY_SOPIN, bytPIN); //写入 int intRandom = new Random().Next(1, 9); //随即取1~8 string key16 = Helper.HMAC_MD5_dog(intRandom, "武汉创方科技"); string encryptString = Helper.RC2Encrypt(origin, key16); //密文 byte[] zyn = System.Text.Encoding.Default.GetBytes(encryptString); ET99_API.et_Write(ET99_API.dogHandle, (ushort)offset, zyn.Length, zyn); //重新进入用户权限 bytPIN = System.Text.Encoding.ASCII.GetBytes(Properties.Resources.UserPIN); ET99_API.et_Verify(ET99_API.dogHandle, ET99_API.ET_VERIFY_USERPIN, bytPIN); }