/// <summary> /// 计算MAC码 /// </summary> /// <param name="source"></param> /// <returns></returns> public string CalculateMAC(StringBuilder source) { byte[] sourceBytes = CodeConvert.HexStringToByteArray(source.ToString()); File.AppendAllText("mac.txt", source.ToString()); StringBuilder sbMac = new StringBuilder(); StringBuilder sbReturn = new StringBuilder(); ZT_EPP.ZT_EPP_ActivWorkPin(0x00, 0x01); ZT_EPP.ZT_EPP_SetDesPara(0x06, 0x03); ZT_EPP.ZT_EPP_PinCalMAC(1, 4, source, sbMac); string mac = CodeConvert.ToHexString(Encoding.ASCII.GetBytes(sbMac.ToString().Substring(0, 8))); return(mac); }
/// <summary> /// 发卡行外部认证 /// </summary> /// <param name="data"></param> /// <returns></returns> public bool IssueBankAuthenticate(string data) { //支付成功获取发卡行返回的55域数据 byte[] part55Data = CodeConvert.HexStringToByteArray(data); List <TLVEntity> tlvList = TLVPackage.Construct(part55Data); TLVEntity entity = TLVHelper.GetValueByTag(tlvList, "91");//获取发卡行认证数据 if (entity != null) { APDUEntity apdu = new APDUEntity("00", "82", "00", "00", CodeConvert.ToHexString(entity.Length) + CodeConvert.ToHexString(entity.Value)); string result = CardReader.SendAPDU(apdu.ToString()); return(true);// result.Equals(StatusCode.Success) == result.Equals(StatusCode.Authenticated); } else { return(true); } }
/// <summary> /// 获取应用文件的数据 /// </summary> /// <param name="dataList">数据列表</param> /// <param name="appFile">应用文件名</param> public void GetDataBySF(List <string> dataList, string appFile) { byte[] sf = CodeConvert.HexStringToByteArray(appFile); int sfi = sf[0]; int startIndex = sf[1]; int endIndex = sf[2]; string body = "00"; for (int i = startIndex; i <= endIndex; i++) { string cla = "00"; string ins = APDU_INS.READ_RECODE; string p1 = i.ToString("x2"); string p2 = (sfi + 4).ToString("x2"); APDUEntity entity = new APDUEntity(cla, ins, p1, p2, body); string result = CardReader.SendAPDU(entity.ToString()); dataList.Add(result); } }
/// <summary> /// IC卡APDU操作 /// </summary> /// <param name="apdu">apdu指令</param> /// <returns></returns> public string SendAPDU(string apdu) { int apduLength = apdu.Length / 2; int dataLength = apduLength + 4; //通讯包长度=4+ C-APDU 包长度 n (n 最大值为 262byte string apduLengthString = apduLength.ToString("x4"); //通讯包长度 2 byte string dataLengthString = dataLength.ToString("x4"); #region Build Cmd List <byte> bytes = new List <byte>(); bytes.Add(0x02); bytes.AddRange(CodeConvert.HexStringToByteArray(dataLengthString)); bytes.Add(0x37); bytes.Add(0x31); bytes.AddRange(CodeConvert.HexStringToByteArray(apduLengthString)); bytes.AddRange(CodeConvert.HexStringToByteArray(apdu)); bytes.Add(0x03); bytes.Add(new byte()); #endregion byte[] cmd = bytes.ToArray(); AppendBcc(cmd); if (!com.IsOpen) { com.OpenPort(); } byte[] result = com.SendCmd(cmd); int apduDataLength = result[6] * 16 * 16 + result[7]; byte[] apduData = result.Skip(8).Take(apduDataLength).ToArray(); com.ClosePort(); string data = CodeConvert.ToHexString(apduData); if (!data.EndsWith("9000")) { throw new CardReadException(string.Format("读卡错误,{0}!", data)); } data = data.Substring(0, data.Length - 4).Trim(); return(data); }