/// <summary> /// 初始化0区,参数:0区新卡号和原密码 /// </summary> public static string WriteICCard(string card, string pwd) { int icdev = 0; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb string checkStr = check(icdev); if (!checkStr.Equals("ok")) { return(checkStr); } int st = ICCardUtility.IC_CheckPass_4442hex(icdev, pwd, 0); //下载密码到读卡器 if (st == 0) { st = ICCardUtility.IC_Write_hex(icdev, 1, len, card); //写卡号 int st1 = ICCardUtility.IC_ChangePass(icdev, 3, 0, pwd, ICPass); //写密码 if (st == 0 && st1 == 0) { return("Success"); } } return("操作失败"); } finally { //对卡下电,对于逻辑加密卡,下电后必须密码变为有效,即要写卡必须重新校验密码。 ICCardUtility.IC_Down(icdev); //关闭端口,在Windows系统中,同时只能有一个设备使用端口,所以在退出系统时,请关闭端口,以便于其它设备使用。 ICCardUtility.IC_ExitComm(icdev); } }
/// <summary> /// 判断读卡器中插入的是一张空卡 /// </summary> public static bool CheckNullCard(string storePassword) { int icdev = 0; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb if (icdev <= 0) { return(false); } int iReadDev = -1; byte[] sdata = new byte[9]; iReadDev = ICCardUtility.IC_ReadVer(icdev, sdata); if (iReadDev < 0) { return(false); } short st = 0; st = ICCardUtility.IC_InitType(icdev, 16); if (st < 0) { return(false); } st = ICCardUtility.IC_DevBeep(icdev, 10);//等待10毫秒 st = ICCardUtility.IC_Status(icdev); if (st == 1) { return(false); } if (st != 0) { return(false); } st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass); if (st != 0) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword); if (st == 0) { return(false); } } return(true); } finally { //对卡下电,对于逻辑加密卡,下电后必须密码变为有效,即要写卡必须重新校验密码。 ICCardUtility.IC_Down(icdev); //关闭端口,在Windows系统中,同时只能有一个设备使用端口,所以在退出系统时,请关闭端口,以便于其它设备使用。 ICCardUtility.IC_ExitComm(icdev); } }
/// <summary> /// 读卡器蜂鸣 /// </summary> public static void Beep(int time) { int icdev = 0; icdev = ICCardUtility.IC_InitComm(100); //初始化usb ICCardUtility.IC_DevBeep(icdev, (uint)time); //等待10毫秒 ICCardUtility.IC_ExitComm(icdev); }
/// <summary> /// 读卡器按会员IC卡号写一张新卡 /// </summary> public static string WriteICCardID(string sNewICCard, string sRepeatCode, string storePassword, bool isBeep = true) { int icdev = 0; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb string checkStr = ""; checkStr = check(icdev, isBeep); //基本检查 if (!checkStr.Equals("ok")) { return(checkStr); } short st = 0; //校验出厂密码 //1区的出厂密码和0区出厂密码不同,0区778852013144 1区FFFFFFFFFFFF st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass_one); if (st != 0) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword); //校验本店密码 if (st != 0) { return("提示:无法通过密码校验,可能不是本店的卡。"); } } else { st = ICCardUtility.IC_ChangePass_4442hex(icdev, pwd_postion, ICPass_one, storePassword); if (st != 0) { return("提示:无法设置IC卡密码。"); } st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword); //校验本店密码 } byte[] b1 = new byte[16]; byte[] b2 = Encoding.ASCII.GetBytes(sNewICCard); Array.Copy(b2, b1, b2.Length); b1[b2.Length] = (byte)Convert.ToInt32(sRepeatCode); st = ICCardUtility.IC_Write(icdev, ic_postion, len, b1); if (st != 0) { return("提示:无法写入IC卡号码。"); } return("Success"); } finally { //对卡下电,对于逻辑加密卡,下电后必须密码变为有效,即要写卡必须重新校验密码。 ICCardUtility.IC_Down(icdev); //关闭端口,在Windows系统中,同时只能有一个设备使用端口,所以在退出系统时,请关闭端口,以便于其它设备使用。 ICCardUtility.IC_ExitComm(icdev); } }
/// <summary> /// 每次插卡时生成新的IC卡序列号 /// </summary> public static string RefreshICCard(string sNewICCard, string sRepeatCode, string storePassword) { int icdev = 0; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb string checkStr = check(icdev); if (!checkStr.Equals("ok")) { return(checkStr); } short st = 0; st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass); if (st != 0) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword); if (st != 0) { return("无法通过密码校验,可能不是本店的卡。"); } } else { return("此卡是一张空白卡不能刷新。"); } byte[] b1 = new byte[16]; byte[] b2 = Encoding.ASCII.GetBytes(sNewICCard); Array.Copy(b2, b1, b2.Length); b1[b2.Length] = (byte)Convert.ToInt32(sRepeatCode); st = ICCardUtility.IC_Write(icdev, ic_postion, len, b1); if (st != 0) { return("无法写入IC卡号码。"); } return("Success"); } finally { //对卡下电,对于逻辑加密卡,下电后必须密码变为有效,即要写卡必须重新校验密码。 ICCardUtility.IC_Down(icdev); //关闭端口,在Windows系统中,同时只能有一个设备使用端口,所以在退出系统时,请关闭端口,以便于其它设备使用。 ICCardUtility.IC_ExitComm(icdev); } }
/// <summary> /// 更改密码(0为更改成功) /// </summary> private static short IC_ChangePass(int icdev, int pwd_postion, int secnr_index, string oldPassWord, string newPassWord) { Int16 result = 0; result = Dcrf32.dc_authentication(icdev, pwd_mode, secnr_index); result = ICCardUtility.IC_CheckPass_4442hex(icdev, oldPassWord, secnr_index); //校验出厂密码 if (result != 0) { return(result); } string data = newPassWord + "FF078069" + newPassWord; result = Dcrf32.dc_write_hex(icdev, pwd_postion, data); return(result); }
/// <summary> /// 基础校验,检查读卡器与IC卡是否都存在 /// 默认蜂鸣true /// 存在返回ok,不存在返回错误信息 /// </summary> private static string check(int icdev, bool isBeep = true) { if (icdev <= 0) { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); return("提示:IC读卡器USB初始化失败!"); } int iReadDev = -1; byte[] sdata = new byte[9]; iReadDev = ICCardUtility.IC_ReadVer(icdev, sdata); if (iReadDev < 0) { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); return("提示:无法找到IC读卡器硬件版本号!"); } short st = 0; st = ICCardUtility.IC_InitType(icdev, 16); if (st < 0) { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); return("提示:设置IC卡类型失败!"); } if (isBeep) { st = ICCardUtility.IC_DevBeep(icdev, 10); //等待10毫秒 } st = ICCardUtility.IC_Status(icdev); if (st == 1) { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); return("提示:读卡器中没有插卡"); } if (st != 0) { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); return("提示:无法连接到读卡器"); } return("ok"); }
public static string GetUid() { int icdev = 0; icdev = ICCardUtility.IC_InitComm(100); string checkStr = check(icdev, false); if (!checkStr.Equals("ok")) { return(checkStr); } //获得IC卡UID ulong uid = 0; int st = dc_anticoll(icdev, ref uid); ICCardUtility.IC_ExitComm(icdev); return(uid.ToString()); }
/// <summary> /// 删除1区数据并恢复出厂密码FFFFFFFFFFFF /// </summary> /// <param name="icdev">读卡器ID</param> /// <param name="pwd">1区密码</param> /// <returns>成功true</returns> private static bool DeleteICCaredData(int icdev, string pwd) { byte[] b = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int st = ICCardUtility.IC_CheckPass_4442hex(icdev, pwd); //下载密码到读卡器 if (st == 0) { st = ICCardUtility.IC_Write(icdev, ic_postion, len, b); //清空卡号 int st1 = ICCardUtility.IC_ChangePass_4442hex(icdev, pwd_postion, pwd, ICPass_one); //恢复原出厂密码 if (st == 0 && st1 == 0) { return(true); } } return(false); }
/// <summary> /// 获取当前读卡器上的IC卡号码,返回8位的卡号或错误信息 /// 此方法自行处理卡号+加动态码,返回最终卡号,与ReadICCard不同 /// </summary> public static bool GetICCardID(string storePassword, out string icCardId, out string repeatCode, out string errMsg, bool isBeep = true, bool isCreate = false) { icCardId = string.Empty; errMsg = string.Empty; string sICCardID = string.Empty; if (!ICCardUtility.ReadICCard(storePassword, out sICCardID, out repeatCode, out errMsg, isBeep, isCreate)) { return(false); } if (IsValidCardNo(sICCardID)) { try { string sRepeatCode = Convert.ToInt32(sICCardID.Substring(8), 16).ToString(); sICCardID = sICCardID.Substring(0, 8); int iICCardID = 0; if (ICCardUtility.isNumberic(sICCardID, out iICCardID)) { icCardId = iICCardID.ToString(); } else { icCardId = sICCardID; } return(true); } catch { errMsg = "读取卡片出错"; return(false); } } else { errMsg = sICCardID; return(false); } return(true); }
/// <summary> /// 读卡器回收卡 /// </summary> public static bool RecoveryICCard(string storePassword, out string errMsg) { int icdev = 0; errMsg = string.Empty; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb string checkStr = check(icdev); if (!checkStr.Equals("ok")) { errMsg = checkStr; return(false); } if (DeleteICCaredData(icdev, ICPass_one)) { return(true); } if (DeleteICCaredData(icdev, storePassword)) { return(true); } if (DeleteICCaredData(icdev, ICPass)) { return(true); } errMsg = "此卡不是新卡也不是本店的卡,无法通过密码校验"; return(true); } finally { //对卡下电,对于逻辑加密卡,下电后必须密码变为有效,即要写卡必须重新校验密码。 ICCardUtility.IC_Down(icdev); //关闭端口,在Windows系统中,同时只能有一个设备使用端口,所以在退出系统时,请关闭端口,以便于其它设备使用。 ICCardUtility.IC_ExitComm(icdev); } }
/// <summary> /// 读取16进制的卡号(含动态码的卡号) /// 参数:isBeep 是否需要蜂鸣, /// isCreate是入库或入会类型的操作,此类操作空白卡需要返回卡号 /// 校验逻辑:1检查设备 2.检查密码 3.检查是不是空白卡 /// 返回包含动态码的完整卡号,开新卡的业务只返回卡号 /// </summary> public static bool ReadICCard(string storePassword, out string icCardId, out string repeatCode, out string errMsg, bool isBeep = true, bool isCreate = false) { string checkStr = ""; icCardId = string.Empty; repeatCode = string.Empty; errMsg = string.Empty; int icdev = 0; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb checkStr = check(icdev, isBeep); //基本检查 if (!checkStr.Equals("ok")) { errMsg = checkStr; return(false); } short st = 0; //开新卡 if (isCreate) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword, secnr_index); if (st == 0) { errMsg = "提示:此卡正在使用。"; return(false); } st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass, secnr_index_ic); if (st != 0) { errMsg = "提示:无法读取IC卡信息。"; return(false); } StringBuilder data = new StringBuilder(); st = ICCardUtility.IC_Read_hex(icdev, 1, len - 1, data); //新卡块4无卡号,改为读块1的原始卡号 if (st == 0) { errMsg = data.ToString().Substring(0, len - 1); return(true); } } else { st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass_one, secnr_index); if (st == 0) { errMsg = "提示:此卡是一张空白卡,不需要处理。"; return(false); } st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword, secnr_index); if (st != 0) { errMsg = "提示:无法通过密码校验,可能不是本店的卡。"; return(false); } byte[] data = new byte[len]; st = ICCardUtility.IC_Read(icdev, ic_postion, len, data); if (st == 0) { byte[] b1 = new byte[len - 1]; Array.Copy(data, b1, b1.Length); byte b2 = data[len - 1]; string s = Encoding.ASCII.GetString(b1) + b2.ToString("X2"); repeatCode = Convert.ToInt32(s.Substring(8), 16).ToString(); string sICCardID = s.Substring(0, 8).Replace("\0", ""); icCardId = s; return(true); } } errMsg = "提示:无法读取IC卡信息。"; return(false); } finally { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); } }
/// <summary> /// 仅仅读取卡号(不校验)参数扇区号0-15,块号0-63 /// </summary> public static string GetCardID(int index, int postion, string storePassword, bool isBeep = false) { string sICCardID = ""; string checkStr = ""; int icdev = 1; try { icdev = ICCardUtility.IC_InitComm(100); //初始化usb checkStr = check(icdev, isBeep); //基本检查 if (!checkStr.Equals("ok")) { return(checkStr); } short st = 0; if (index == 0) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass, index); if (st != 0) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass_one, index); if (st != 0) { return("提示:无法读取IC卡信息。"); } } StringBuilder data = new StringBuilder(); st = ICCardUtility.IC_Read_hex(icdev, postion, 8, data); //新卡块4无卡号,改为读块1的原始卡号 if (st == 0) { return(data.ToString().Substring(0, 8)); } return("提示:无法读取IC卡信息。"); } else { st = ICCardUtility.IC_CheckPass_4442hex(icdev, storePassword, index); if (st != 0) { st = ICCardUtility.IC_CheckPass_4442hex(icdev, ICPass_one, index); if (st != 0) { return("提示:无法读取IC卡信息。"); } } byte[] data = new byte[8]; st = ICCardUtility.IC_Read(icdev, postion, 8, data); if (st == 0) { sICCardID = Encoding.ASCII.GetString(data).Replace("\0", "0"); return(sICCardID); } return("提示:无法读取IC卡信息。"); } } finally { ICCardUtility.IC_Down(icdev); ICCardUtility.IC_ExitComm(icdev); } }