Esempio n. 1
0
        /// <summary>
        /// 外部身份验证
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public ApduMsg Auth(byte[] Key)
        {
            if (Key.Length != 8)
            {
                return(new ApduMsg("身份验证Key长度不正确"));
            }
            string RANDOMCMD = "0084000004";


            byte[] Random = SendStrCommand(RANDOMCMD);

            CPUCardLogHelper.AddLog("获取随机数:" + BitConverter.ToString(Random));

            if (Random.Length != 6)
            {
                return(new ApduMsg("返回随机数长度不正确"));
            }

            byte[] Randomdata = new byte[8];

            Array.Copy(Random, 0, Randomdata, 0, 4);

            List <byte> cmdList = new List <byte>();

            cmdList.AddRange(CPUCardHelper.ConverToBytes("00 82 00 00 08"));
            cmdList.AddRange(CPUCardHelper.Encrypt(Randomdata, Key));

            byte[] data = carder.SendCommand(cmdList.ToArray());

            CPUCardLogHelper.AddLog(LogTypeEnum.info, "身份验证接收", BitConverter.ToString(data));

            return(ApduMsg.GetApduByData(data));
        }
Esempio n. 2
0
        /// <summary>
        ///发送命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public byte[] SendCommand(byte[] cmd)
        {
            byte[] result = new byte[0];

            if (!DevicesStatus)
            {
                if (!OpenReader(out string msg))
                {
                    throw new Exception(msg);
                    return(result);
                }
            }
            CPUCardLogHelper.AddLog(LogTypeEnum.Send, "", cmd);

            StringBuilder temp1 = new StringBuilder(64);

            byte[] rbuff = new byte[256];
            byte   rlen  = 0;

            int st = dc_pro_command(icdev, (byte)cmd.Length, cmd, ref rlen, rbuff, (byte)7);

            if (st != 0)
            {
                //throw new Exception("设备发送失败");
                dc_exit(icdev);
                return(result);
            }
            result = new byte[rlen];
            Array.Copy(rbuff, 0, result, 0, result.Length);
            CPUCardLogHelper.AddLog(LogTypeEnum.Recivie, "", result);

            if (NeedBeep)
            {
                //由于程序每发送一次指令响一次,太频繁,也影响速度。所以每BeepTimes一次才响一次。
                if (curBeep == 0)
                {
                    Beep();
                }
                curBeep++;
                if (curBeep >= BeepTimes)
                {
                    curBeep = 0;
                }
            }

            return(result);
        }
Esempio n. 3
0
        public byte[] SendCommand(byte[] command)
        {
            //  return SendCommand2(command);

            using (RfidReader.Transaction(SCardReaderDisposition.Leave))
            {
                CPUCardLogHelper.AddLog(LogTypeEnum.Send, "", command);

                var sendPci = SCardPCI.GetPci(RfidReader.Protocol);

                var receivePci = new SCardPCI(); // IO returned protocol control information.

                var receiveBuffer = new byte[256];
                try
                {
                    var bytesReceived = RfidReader.Transmit(
                        sendPci,               // Protocol Control Information (T0, T1 or Raw)
                        command,               // command APDU
                        command.Length,
                        receivePci,            // returning Protocol Control Information
                        receiveBuffer,
                        receiveBuffer.Length); // data buffer

                    //var responseApdu = new ResponseApdu(receiveBuffer, bytesReceived, IsoCase.Case2Short, rfidReader.Protocol);

                    byte[] receiveData = new byte[bytesReceived];

                    //byte[] result = responseApdu.GetData();

                    Array.Copy(receiveBuffer, receiveData, bytesReceived);

                    CPUCardLogHelper.AddLog(LogTypeEnum.Recivie, "", receiveData);

                    return(receiveData);
                }
                catch (Exception ex)
                {
                    CPUCardLogHelper.AddLog(LogTypeEnum.error, "设备发送异常" + ex.Message, command);
                    return(new byte[0]);
                }
            }
        }