public bool ExtAuth(string extPsd) { bool bRet = false; try { Log.Debug("购电外部认证" + extPsd); byte[] bOut = new byte[2048]; int nOut = 0; string info = ""; byte[] bExt = PubFunc.HexStringToByteArray(extPsd); byte[] bCommand = new byte[EXT_AUTH.Length + bExt.Length]; Array.Copy(EXT_AUTH, bCommand, EXT_AUTH.Length); Array.Copy(bExt, 0, bCommand, EXT_AUTH.Length, bExt.Length); Log.Debug("购电外部命令:" + PubFunc.ByteArrayToHexString(bCommand, bCommand.Length)); CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("购电外部:" + info); if (info == RET_STRING) { bRet = true; } } } catch (Exception ex) { Log.Error("ExtAuth:" + ex.ToString()); } return(bRet); }
public bool UpdateKey(string key) { bool bRet = false; try { byte[] bOut = new byte[2048]; string sKey = "84D401FF20"; int nOut = 0; string info = ""; byte[] bCommand = PubFunc.HexStringToByteArray(sKey + key); CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); if (info == RET_STRING) { bRet = true; } } } catch (Exception ex) { Log.Error("UpdateKey:" + ex); } return(bRet); }
private byte[] EXT_AUTH = { 0x00, 0x82, 0x00, 0x04, 0x08 }; //外部认证 + 密钥 /// <summary> /// 获取电卡信息 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetCardInfo(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "5DDC19FC1287506465BE2C70D5080817C28C009B181DA1C46ADC7285F5D1FCCD7CD2E7CAE3EA664F5754B8AC2C6DF4189B14A56696A950B915BE6794C7F49E41849C8756619EA31922FE5D04326128B03A32FB122697F077F932274EF10E320790EAF47E1304D7BF33DC2176D9FFE4E2F29069BF5C8E3D47ED9CAC426C38ADC69000"; return(true); } try { byte[] bOut = new byte[2048]; int nOut = 0; CardReader.Status sRet = CardReader.CardChipIO(GET_CARD_INFO, GET_CARD_INFO.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("GetCardInfo:" + info); if (info.Substring(info.Length - 4) == RET_STRING) { bRet = true; info = info.Substring(0, info.Length - 4); } else { Log.Warn("GetCardInfo:" + info); } } } catch (Exception ex) { Log.Error("GetCardInfo:" + ex); } return(bRet); }
public bool LimitAuth(string limitPsd) { bool bRet = false; try { Log.Debug("写权限认证:" + limitPsd); byte[] bOut = new byte[2048]; int nOut = 0; string info = ""; byte[] bLimit = PubFunc.HexStringToByteArray(limitPsd); byte[] bCommand = new byte[LIMIT_AUTH.Length + bLimit.Length]; Array.Copy(LIMIT_AUTH, bCommand, LIMIT_AUTH.Length); Array.Copy(bLimit, 0, bCommand, LIMIT_AUTH.Length, bLimit.Length); //bCommand[bCommand.Length - 1] = 0x00; Log.Debug("写权限命令:" + PubFunc.ByteArrayToHexString(bCommand, bCommand.Length)); CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("写权限:" + info); if (info == RET_STRING) { bRet = true; } } } catch (Exception ex) { Log.Error("LimitAuth:" + ex.ToString()); } return(bRet); }
/// <summary> /// 8. 写返写区文件 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool SetEF5(string ef5) { bool bRet = false; try { byte[] bOut = new byte[2048]; string sCommand = "04D6850035"; int nOut = 0; string info = ""; byte[] bCommand = PubFunc.HexStringToByteArray(sCommand + ef5); CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); if (info == RET_STRING) { bRet = true; } } } catch (Exception ex) { Log.Error("SetEF5:" + ex); } return(bRet); }
/// <summary> /// 读钱包文件 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetEF2(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "0000283C000000019000"; return(true); } try { byte[] bOut = new byte[2048]; int nOut = 0; CardReader.Status sRet = CardReader.CardChipIO(GET_EF2, GET_EF2.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("GetEF2:" + info); if (info.Substring(info.Length - 4) == RET_STRING) { bRet = true; info = info.Substring(0, info.Length - 4); } else { Log.Warn("GetEF2:" + info); } } } catch (Exception ex) { Log.Error("GetEF2:" + ex); } return(bRet); }
/// <summary> /// 8. 读返写区文件 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetEF5(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "6811002B02000001000001000000629276002013007233000017E80000001700000000010004010000001510051026FE169000"; return(true); } try { byte[] bOut = new byte[2048]; int nOut = 0; CardReader.Status sRet = CardReader.CardChipIO(GET_EF5, GET_EF5.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("GetEF5:" + info); if (info.Substring(info.Length - 4) == RET_STRING) { bRet = true; info = info.Substring(0, info.Length - 4); } else { Log.Warn("GetEF5:" + info); } } } catch (Exception ex) { Log.Error("GetEF5:" + ex); } return(bRet); }
/// <summary> /// 上电获取卡片序列号 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetCardNo(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "10110800000F82CA"; return(true); } try { byte[] bAnswer = new byte[512]; int nLen = 0; int nChip = 0; CardReader.Status status = CardReader.CardPowerUp(bAnswer, ref nLen, ref nChip); Log.Error("GetCardResult:" + Enum.GetName(typeof(CardReader.Status), status)); if (status == CardReader.Status.CARD_SUCC) { bRet = true; info = PubFunc.ByteArrayToHexString(bAnswer, nLen); Log.Debug("GetCardNO:" + info); info = info.Substring(info.Length - 16); } } catch (Exception ex) { Log.Error("GetCardNo:" + ex); } return(bRet); }
/// <summary> /// 判断电卡类型 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetCardType(ref string info) { bool bRet = false; try { byte[] bOut = new byte[2048]; int nOut = 0; CardReader.Status sRet = CardReader.CardChipIO(GET_CARD_TYPE, GET_CARD_TYPE.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("GetCardType:" + info); if (info == RET_CARDTYPE_2) { bRet = true; } else { Log.Warn("GetCardType:" + info); } } } catch (Exception ex) { Log.Error("GetCardType:" + ex); } return(bRet); }
/// <summary> /// 提取TLV标签 /// string str9F10 = pboc.App_EMVGetData("9F10", ref iValueLen, ref strValue); /// /// </summary> /// <param name="strTag"></param> /// <param name="iValueLenSet">期望长度</param> /// <param name="iValueLen"></param> /// <param name="strValue"></param> /// <returns></returns> public string App_EMVGetData(string strTag, int iValueLenSet, ref int iValueLen, ref string strValue) { try { string strRet = ""; byte[] ptag = PubFunc.HexStringToByteArray(strTag); byte[] res = new byte[2048]; int retLen = 0; int tagLen = ptag.Length; EMVGetDate(ptag, tagLen, res, ref retLen); byte[] res2 = new byte[retLen]; Array.Copy(res, res2, retLen); strValue = PubFunc.ByteArrayToHexString(res2, res2.Length); iValueLen = retLen; if (iValueLenSet > 0 && iValueLen == 0) { strValue = strValue.PadRight(iValueLenSet * 2, '0'); iValueLen = iValueLenSet; } strRet = String.Format("{0}{1:X2}{2}", strTag, iValueLen, strValue); return(strRet); } catch { throw; } }
/// <summary> /// 提取TLV标签 /// string str9F10 = pboc.App_EMVGetData("9F10", ref iValueLen, ref strValue); /// </summary> /// <param name="strTag"></param> /// <param name="iValueLen"></param> /// <param name="strValue"></param> /// <returns></returns> public string App_EMVGetData(string strTag, ref int iValueLen, ref string strValue) { try { //PubFunc pubCom = new PubFunc(); string strRet = ""; byte[] ptag = PubFunc.HexStringToByteArray(strTag); byte[] res = new byte[2048]; int retLen = 0; int tagLen = ptag.Length; EMVGetDate(ptag, tagLen, res, ref retLen); byte[] res2 = new byte[retLen]; Array.Copy(res, res2, retLen); strValue = PubFunc.ByteArrayToHexString(res2, res2.Length); iValueLen = retLen; strRet = String.Format("{0}{1:X2}{2}", strTag, iValueLen, strValue); return(strRet); } catch { throw; } }
//#region 购电认证 old //public bool CertAuth(string random,string certPsd) //{ // bool bRet = false; // try // { // byte[] bOut = new byte[2048]; // byte[] bOut1 = new byte[2048]; // int nOut = 0; // string info = ""; // byte[] bRandom = PubFunc.HexStringToByteArray(random); // byte[] bCommand = new byte[CERT_AUTH.Length + bRandom.Length]; // Array.Copy(CERT_AUTH, bCommand, CERT_AUTH.Length); // Array.Copy(bRandom, 0, bCommand, CERT_AUTH.Length, bRandom.Length); // //计算随机数 // CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); // if (sRet == CardReader.Status.CARD_SUCC) // { // info = PubFunc.ByteArrayToHexString(bOut, nOut); // if (info != RET_STRING && info == RET_STRING2) // return false; // nOut = 0; // //校验身份验证数据 // sRet = CardReader.CardChipIO(CERT_AUTH_GET, CERT_AUTH_GET.Length, bOut1, ref nOut); // if (sRet == CardReader.Status.CARD_SUCC) // { // info = PubFunc.ByteArrayToHexString(bOut1, nOut); // if (info.Substring(info.Length - 4) == RET_STRING) // { // info = info.Substring(0, info.Length - 4); // bRet = string.Compare(info, certPsd, true) == 0; // } // } // } // } // catch (Exception ex) // { // Log.Error("CertAuth:" + ex); // } // return bRet; //} //public bool LimitAuth(string limitPsd) //{ // bool bRet = false; // try // { // byte[] bOut = new byte[2048]; // int nOut = 0; // string info = ""; // byte[] bLimit = PubFunc.HexStringToByteArray(limitPsd); // byte[] bCommand = new byte[LIMIT_AUTH.Length + bLimit.Length]; // Array.Copy(LIMIT_AUTH, bCommand, LIMIT_AUTH.Length); // Array.Copy(bLimit, 0, bCommand, LIMIT_AUTH.Length, bLimit.Length); // CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); // if (sRet == CardReader.Status.CARD_SUCC) // { // info = PubFunc.ByteArrayToHexString(bOut, nOut); // if (info == RET_STRING) // { // bRet = true; // } // } // } // catch (Exception ex) // { // Log.Error("LimitAuth:" + ex); // } // return bRet; //} //public bool ExtAuth(string extPsd) //{ // bool bRet = false; // try // { // byte[] bOut = new byte[2048]; // int nOut = 0; // string info = ""; // byte[] bExt = PubFunc.HexStringToByteArray(extPsd); // byte[] bCommand = new byte[EXT_AUTH.Length + bExt.Length]; // Array.Copy(EXT_AUTH, bCommand, EXT_AUTH.Length); // Array.Copy(bExt, 0, bCommand, EXT_AUTH.Length, bExt.Length); // CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); // if (sRet == CardReader.Status.CARD_SUCC) // { // info = PubFunc.ByteArrayToHexString(bOut, nOut); // if (info == RET_STRING) // { // bRet = true; // } // } // } // catch (Exception ex) // { // Log.Error("ExtAuth:" + ex); // } // return bRet; //} //#endregion #region 购电认证 public bool CertAuth(string random, string certPsd) { bool bRet = false; try { Log.Debug("身份认证"); byte[] bOut = new byte[2048]; //byte[] bOut1 = new byte[2048]; int nOut = 0; string info = ""; byte[] bRandom = PubFunc.HexStringToByteArray(random); byte[] bCommand = new byte[CERT_AUTH.Length + bRandom.Length]; Array.Copy(CERT_AUTH, bCommand, CERT_AUTH.Length); Array.Copy(bRandom, 0, bCommand, CERT_AUTH.Length, bRandom.Length); Log.Debug("身份认证命令:" + PubFunc.ByteArrayToHexString(bCommand, bCommand.Length)); //计算随机数 CardReader.Status sRet = CardReader.CardChipIO(bCommand, bCommand.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); if (info != RET_STRING && info == RET_STRING2) { return(false); } //nOut = 0; ////校验身份验证数据 //sRet = CardReader.CardChipIO(CERT_AUTH_GET, CERT_AUTH_GET.Length, bOut1, ref nOut); //if (sRet == CardReader.Status.CARD_SUCC) //{ // info = PubFunc.ByteArrayToHexString(bOut1, nOut); // if (info.Substring(info.Length - 4) == RET_STRING) // { info = info.Substring(0, info.Length - 4); Log.Info("CertAuth:" + info + " CertPsd:" + certPsd); bRet = String.Compare(info, certPsd, StringComparison.OrdinalIgnoreCase) == 0; //} //} } } catch (Exception ex) { Log.Error("CertAuth:" + ex.ToString()); } return(bRet); }
/// <summary> /// 交易完后与IC进行交互 /// </summary> /// <param name="sendField55"></param> /// <param name="state"></param> /// <returns></returns> private bool ICTransFunc(ref int state) { bool bRet = false; try { CR.CreateReverseFile();//建立冲正文件 string temp = PubFunc.ByteArrayToHexString(EMVInfo.RecvField55, EMVInfo.RecvField55.Length); state = pboc.App_EMVOnlineDataProcess(0, Encoding.Default.GetBytes(temp), temp.Length, PubFunc.HexStringToByteArray(EMVInfo.RecvField38), EMVInfo.RecvField38.Length); CR.ClearReverseFile();//清除冲正文件 bRet = true; } catch (System.Exception ex) { CR.DoReverseFile();//发起冲正 CardReader.CardPowerDown(); Log.Error("App_EMVOnlineDataProcess Error!", ex); } return(bRet); }
/// <summary> /// 选择电卡文件 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool SelectDirectory(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "6F0A8400A50688009F0801029000"; return(true); } try { byte[] bOut = new byte[2048]; int nOut = 0; CardReader.Status sRet = CardReader.CardChipIO(SELECT_FILE, SELECT_FILE.Length, bOut, ref nOut); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("SelectDirectory:" + info); string temp = info.Substring(info.Length - 4); if (temp == RET_STRING || temp == RET_STRING1) { bRet = true; info = info.Substring(0, info.Length - 4); } else { Log.Warn("SelectDirectory:" + info); } } } catch (Exception ex) { Log.Error("SelectDirectory:" + ex); } return(bRet); }
/// <summary> /// 随机数 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool GetRandom(ref string info) { bool bRet = false; if (!CardReader.IsUse) { info = "C97FFA2376FD9E43"; return(true); } try { byte[] bOut = new byte[2048]; int nOut = 0; CardReader.Status sRet = CardReader.CardChipIO(GET_RANDOM, GET_RANDOM.Length, bOut, ref nOut); Log.Debug("GetRandom Status:" + Enum.GetName(typeof(CardReader.Status), sRet)); if (sRet == CardReader.Status.CARD_SUCC) { info = PubFunc.ByteArrayToHexString(bOut, nOut); Log.Debug("GetRandom:" + info); if (info.Substring(info.Length - 4) == RET_STRING) { info = info.Substring(0, info.Length - 4); bRet = true; } else { Log.Warn("GetRandom:" + info); } } } catch (Exception ex) { Log.Error("GetRandom:" + ex); } return(bRet); }