예제 #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
        /// <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);
        }
예제 #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>
        /// 提取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;
            }
        }
예제 #6
0
        /// <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;
            }
        }
예제 #7
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);
        }
예제 #8
0
        /// <summary>
        /// 写电卡文件
        /// </summary>
        /// <param name="EF1"></param>
        /// <param name="EF2"></param>
        /// <param name="EF31"></param>
        /// <param name="EF32"></param>
        /// <param name="EF41"></param>
        /// <param name="EF42"></param>
        /// <param name="EF5"></param>
        /// <returns></returns>
        public bool SetFileInfo(PowerCardInfo efInfo)
        {
            bool bRet = false;

            try
            {
                string temp        = efInfo.W_EF1.Substring(10);
                byte   bUpdateFlag = PubFunc.HexStringToByteArray(temp)[0];
                if (((bUpdateFlag >> 7) & 1) == 1)
                {
                    if (!SetEF1(efInfo.W_EF1))
                    {
                        return(false);
                    }
                }

                if (((bUpdateFlag >> 0) & 1) == 1)
                {
                    bRet = SetEF31(efInfo.W_EF31) ? SetEF32(efInfo.W_EF32) : false;
                    if (!bRet)
                    {
                        return(false);
                    }
                }

                if (((bUpdateFlag >> 1) & 1) == 1)
                {
                    bRet = SetEF41(efInfo.W_EF41) ? SetEF42(efInfo.W_EF42) : false;
                    if (!bRet)
                    {
                        return(false);
                    }
                }

                bRet = SetEF2(efInfo.W_EF2) ? SetEF5(efInfo.W_EF5) : false;
            }
            catch (Exception ex)
            {
                Log.Error("SetFileInfo:" + ex);
            }
            return(bRet);
        }
예제 #9
0
        /// <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);
        }