Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        /// <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);
        }
Пример #6
0
        /// <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);
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        /// <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);
        }
Пример #9
0
        //#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);
        }
Пример #10
0
        /// <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);
        }
Пример #11
0
        /// <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);
        }