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