Esempio n. 1
0
        ///// <summary>
        ///// 外接写卡函数
        ///// </summary>
        ///// <param name="cardinfo"></param>
        ///// <param name="gasGardType"></param>
        ///// <returns></returns>
        public static int WriteCard(cardInfo cardinfo, int gasGardType)
        {
            if (!IsUse)
            {
                return(0);
            }

            cardInfo cardinfoTemp = new cardInfo();
            string   outError     = "";
            int      res          = 0;

            try
            {
                if (gasGardType == 0)
                {
                    //天信
                    StringBuilder source      = new StringBuilder(cardinfo.strDeCrypt);
                    StringBuilder destination = new StringBuilder(65);
                    IcSmartCard.XinaoEnCrypt(source, destination);
                    string path = "Tancy_IC.dll";
                    int    port = -1;
                    if (Port.StartsWith("COM"))
                    {
                        port = int.Parse(Port.Substring(3)) - 1;
                    }
                    //int comHz = int.Parse(text_baud.Text);
                    byte[] sPassWord = Encoding.ASCII.GetBytes(destination.ToString());
                    int    iGasCount = int.Parse(cardinfo.gasCount);
                    int    iIcErroy  = -1;
                    string sErrMsg   = null;
                    IcSmartCard.WriteCard(path, port, Bps, sPassWord, ref iGasCount, ref iIcErroy, ref sErrMsg);
                    res = iIcErroy;
                    //res = GasCardReader0.EnCrypt(cardinfo.strDeCrypt, ref cardinfoTemp.strEnCrypt);
                    if (res == 0)
                    {
                        //res = IcSmartCard.WriteCard(Port.ToString(), Bps.ToString(), cardinfoTemp.strEnCrypt, cardinfo.gasCount + 1, ref outError);
                        return(res);
                    }
                    else
                    {
                        throw new Exception("天信写卡错误:" + sErrMsg);
                    }
                }
                else if (gasGardType == 1)
                {
                    //苍南
                    res = GasCardReader1.EnCrypt(cardinfo.strDeCrypt, ref cardinfoTemp.strEnCrypt);
                    Log.Info("write enCrypt: " + cardinfoTemp.strEnCrypt);

                    if (res == 0)
                    {
                        res = GasCardReader1.WriteCard(Port.ToString(), Bps.ToString(), cardinfoTemp.strEnCrypt, cardinfo.gasCount, ref outError);
                        return(res);
                    }
                    else
                    {
                        throw new Exception("GasCardReader1.EnCrypt err");
                    }
                }
                else
                {
                    throw new Exception("cardType Err");
                }
                //else
                //{
                //    res = GasCardReader1.EnCrypt(cardinfo.strDeCrypt, ref cardinfoTemp.strEnCrypt);
                //    if (res == 1)
                //    {
                //        res = GasCardReader0.EnCrypt(cardinfo.strDeCrypt, ref cardinfoTemp.strEnCrypt);
                //        if (res != 0)
                //            throw new Exception("GasCardReader0.EnCrypt err");
                //        res = GasCardReader0.WriteCard(Port.ToString(), Bps.ToString(), cardinfoTemp.strEnCrypt, cardinfo.gasCount + 1,ref outError);
                //        return res;
                //    }
                //    else if (res == 0)
                //    {
                //        res = GasCardReader1.WriteCard(Port.ToString(), Bps.ToString(), cardinfoTemp.strEnCrypt, cardinfo.gasCount + 1,ref outError);
                //        return res;
                //    }
                //    else
                //    {
                //        throw new Exception("GasCardReader1.EnCrypt err");
                //    }
                //}
            }
            catch (Exception ex)
            {
                Log.Error("[GasCardReaderHelper][WriteCard]Error!\n" + ex.ToString());
                return(-1);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 外接读卡函数
        /// </summary>
        /// <param name="cardinfo"></param>
        /// <returns></returns>
        public static int ReadGasCard(ref cardInfo cardinfo)
        {
            //if (!IsUse) return 0;
            if (!IsUse)
            {
                int resDebug = 0;
                cardinfo.cardNo   = "2063838";
                cardinfo.cardType = "1";
                cardinfo.gasCount = "1";
                cardinfo.iccSpare = "0";
                cardinfo.icErroy  = "";
                cardinfo.icNum    = "1";

                return(0);
            }

            cardInfo cardinfoTemp = new cardInfo();
            string   enCrypt      = "";
            int      res          = 0;

            try
            {
                //苍南表读卡
                res = GasCardReader1.ReadCard(Port, Bps.ToString(), ref cardinfoTemp.cardNo, ref cardinfoTemp.gasCount, ref cardinfoTemp.iccSpare, ref cardinfoTemp.icNum, ref cardinfoTemp.icMark, ref cardinfoTemp.strEnCrypt, ref cardinfoTemp.icErroy);
                if (res == 6)
                {
                    //天信表读卡
                    int port = -1;
                    if (Port.StartsWith("COM"))
                    {
                        port = int.Parse(Port.Substring(3)) - 1;
                    }
                    byte[] sCardId   = new byte[200];
                    int    iGasCount = 0;
                    int    iICCSpare = 0;
                    int    iIcNum    = 0;
                    int    iIcMark   = 0;
                    byte[] sPassWord = new byte[200];
                    int    iIcErroy  = -1;
                    string sErrMsg   = null;

                    res = IcSmartCard.ReadCard("Tancy_IC.dll", port, Bps, sCardId, ref iGasCount, ref iICCSpare, ref iIcNum, ref iIcMark, sPassWord, ref iIcErroy, ref sErrMsg);
                    res = iIcErroy;
                    //cardinfoTemp.icErroy = sErrMsg;

                    string cardID   = System.Text.Encoding.Default.GetString(sCardId).Replace("\0", "");
                    string passWord = System.Text.Encoding.Default.GetString(sPassWord).Replace("\0", "");
                    if (sErrMsg != null)
                    {
                        sErrMsg = (sErrMsg).Replace("\0", "");
                    }
                    cardinfoTemp.icErroy = sErrMsg;
                    if (iIcErroy != 0)
                    {
                        return(res);
                    }
                    cardinfoTemp.cardNo   = string.IsNullOrEmpty(cardID) ? "" : cardID;
                    cardinfoTemp.gasCount = string.IsNullOrEmpty(iGasCount.ToString()) ? "" : iGasCount.ToString();
                    cardinfoTemp.iccSpare = string.IsNullOrEmpty(iICCSpare.ToString()) ? "" : iICCSpare.ToString();
                    cardinfoTemp.icNum    = string.IsNullOrEmpty(iIcNum.ToString()) ? "" : iIcNum.ToString();
                    cardinfoTemp.icMark   = string.IsNullOrEmpty(iIcMark.ToString()) ? "" : iIcMark.ToString();
                    if (!string.IsNullOrEmpty(passWord.ToString()))
                    {
                        cardinfoTemp.strEnCrypt = passWord.ToString();
                        StringBuilder temp = new StringBuilder(65);
                        StringBuilder pass = new StringBuilder(cardinfoTemp.strEnCrypt);
                        IcSmartCard.XinaoDeCrypt(pass, temp);
                        cardinfoTemp.strDeCrypt = temp.ToString();
                    }
                    else
                    {
                        cardinfoTemp.strEnCrypt = "";
                        cardinfoTemp.strDeCrypt = "";
                    }
                    //res = GasCardReader0.ReadCard(Port, Bps.ToString(), ref cardinfoTemp.cardNo, ref cardinfoTemp.gasCount, ref cardinfoTemp.iccSpare, ref cardinfoTemp.icNum, ref cardinfoTemp.icMark, ref cardinfoTemp.strEnCrypt, ref cardinfoTemp.icErroy);

                    cardinfoTemp.cardType = "0";
                    //res = GasCardReader1.DeCrypt(cardinfoTemp.strEnCrypt, ref cardinfoTemp.strDeCrypt);
                    //if (res != 64)
                    //    throw new Exception("GasCardReader0.DeCrypt err");
                    cardinfo = cardinfoTemp;
                    //res = 0;
                    return(res);
                }
                else if (res == 0)
                {
                    cardinfoTemp.cardType = "1";
                    res = GasCardReader1.DeCrypt(cardinfoTemp.strEnCrypt, ref cardinfoTemp.strDeCrypt);
                    if (res != 64)
                    {
                        throw new Exception("GasCardReader1.DeCrypt err");
                    }
                    //Log.Info("read De before:" + cardinfoTemp.strDeCrypt);

                    //Log.Info("read En before:" + cardinfoTemp.strEnCrypt);

                    cardinfoTemp.strDeCrypt = cardinfoTemp.strDeCrypt.Substring(0, 64);
                    cardinfoTemp.strEnCrypt = cardinfoTemp.strEnCrypt.Substring(0, 128);
                    Log.Info("read De:" + cardinfoTemp.strDeCrypt);
                    Log.Info("read En:" + cardinfoTemp.strEnCrypt);

                    cardinfo = cardinfoTemp;
                    res      = 0;
                    return(res);
                }
                else
                {
                    return(res);
                }
            }
            catch (Exception ex)
            {
                Log.Error("[GasCardReaderHelper][ReadGasCard]Error!\n" + ex.ToString());
                return(-1);
            }
        }