Ejemplo n.º 1
0
        public void ReadCard()
        {
            //read balance   80 32 00 03 04 00 00 00 00
            //received:                                     Deposit(BCD)
            //                                              vvvvv
            // 05 11 11 25 02 03 12 15 37 20 00 25 09 08 60 00 00 0A 00 18 00 00 00 00 90 00
            //    ^^^^^^^^^^^^^^^^^^^^^^^          ^^^^^ ^^
            //            CAN                issue date  months of validity
            //                               2009/08     60 mths

            APDUCommand apduCmd = new APDUCommand(0x80, 0x32, 0, 3, new byte[4], 0);
            APDUResponse apdu1 = iCard.Transmit(apduCmd);
            byte[] balanceBytes = new byte[4];
            balanceBytes[0] = apdu1.Data[3];
            balanceBytes[1] = apdu1.Data[2];
            balanceBytes[2] = apdu1.Data[1];
            balanceBytes[3] = apdu1.Data[0];
            //balance is in cents. divide by 100 to get dollars
            Balance = BitConverter.ToInt32(balanceBytes, 0) / 100.0f;

            //dunno what the heck is this command, probably selecting something
            apduCmd = new APDUCommand(0, 0xa4, 2, 0, new byte[] { 0, 1 }, 0);
            apdu1 = iCard.Transmit(apduCmd);

            //read CAN, expiry date, deposit 00 B0 00 00 18
            apduCmd = new APDUCommand(0x00, 0xb0, 0, 0, null, 0x18);
            apdu1 = iCard.Transmit(apduCmd);
            //card number (CAN)
            Number = ConvertBCDCardNumber(apdu1.Data, 1, 8);
            //get the string form XXXX-XXXX-XXXX-XXXX
            StringBuilder sb = new StringBuilder();
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 1, 2).ToString("D4"));
            sb.Append("-");
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 3, 2).ToString("D4"));
            sb.Append("-");
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 5, 2).ToString("D4"));
            sb.Append("-");
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 7, 2).ToString("D4"));
            NumberString = sb.ToString();
            //read expiry date
            int year = apdu1.Data[12]; //year, from 1900 or 2000
            if (year > 90 && year < 100)
                year += 1900;
            else year += 2000;
            byte month = apdu1.Data[13];
            byte validity = (byte)ConvertBCDCardNumber(apdu1.Data, 14, 1); //validity in months
            DateTime issueDate = new DateTime(year, month, 1);
            Expiry = issueDate.AddMonths(validity);
            ExpiryString = Expiry.ToString("dd MMM yyyy");
            Deposit = ConvertBCDCardNumber(apdu1.Data, 15, 2) / 100.0f;

            //get transaction log
            //read number of total entries in log
            apduCmd = new APDUCommand(0, 0xb0, 0, 2, null, 4);
            apdu1 = iCard.Transmit(apduCmd);
            if (apdu1.SW1 != 0x90) throw new Exception("Read NETS log size fail");
            short logSize = (short)ConvertBCDCardNumber(new byte[]{apdu1.Data[2], apdu1.Data[3]}, 0, 2);

            apduCmd = new APDUCommand(0, 0xa4, 2, 0, new byte[] { 0, 5 }, 0);
            apdu1 = iCard.Transmit(apduCmd);
            if (apdu1.SW1 != 0x90) throw new Exception("Read NETS transaction fail");

            //read the current log entry number
            short endOffs = (short)(logSize * 4);
            byte endP1 = (byte)((endOffs >> 8) & 0xff);
            byte endP2 = (byte)(endOffs & 0xff);
            apduCmd = new APDUCommand(0, 0xb0, endP1, endP2, null, 2);
            apdu1 = iCard.Transmit(apduCmd);
            if (apdu1.SW1 != 0x90) throw new Exception("Read NETS current log number fail");
            short currentLogNum = (short)ConvertBCDCardNumber(new byte[] { apdu1.Data[0], apdu1.Data[1] }, 0, 2);
            currentLogNum -= 2;

            //read each log entry from the ring buffer, starting from current
            for (byte n = 0; n < logSize; n++)
            {
                int i = currentLogNum - n;
                if (i < 0) i += logSize;
                short logOffs = (short)(i * 4);
                byte logP1 = (byte)((logOffs >> 8) & 0xff);
                byte logP2 = (byte)(logOffs & 0xff);
                //read binary 0xb0, offset logOffs, size 16
                apduCmd = new APDUCommand(0, 0xb0, logP1, logP2, null, 16);
                apdu1 = iCard.Transmit(apduCmd);
                if (apdu1.SW1 != 0x90) throw new Exception("Read NETS log entry fail");
                NETSTransaction logEntry = new NETSTransaction(apdu1.Data);
                if (logEntry.Type != 0)
                    TransactionLog.Add(logEntry);
            }
        }
Ejemplo n.º 2
0
        public void ReadCard()
        {
            //read balance   80 32 00 03 04 00 00 00 00
            //received:                                     Deposit(BCD)
            //                                              vvvvv
            // 05 11 11 25 02 03 12 15 37 20 00 25 09 08 60 00 00 0A 00 18 00 00 00 00 90 00
            //    ^^^^^^^^^^^^^^^^^^^^^^^          ^^^^^ ^^
            //            CAN                issue date  months of validity
            //                               2009/08     60 mths

            APDUCommand  apduCmd = new APDUCommand(0x80, 0x32, 0, 3, new byte[4], 0);
            APDUResponse apdu1   = iCard.Transmit(apduCmd);

            byte[] balanceBytes = new byte[4];
            balanceBytes[0] = apdu1.Data[3];
            balanceBytes[1] = apdu1.Data[2];
            balanceBytes[2] = apdu1.Data[1];
            balanceBytes[3] = apdu1.Data[0];
            //balance is in cents. divide by 100 to get dollars
            Balance = BitConverter.ToInt32(balanceBytes, 0) / 100.0f;

            //dunno what the heck is this command, probably selecting something
            apduCmd = new APDUCommand(0, 0xa4, 2, 0, new byte[] { 0, 1 }, 0);
            apdu1   = iCard.Transmit(apduCmd);

            //read CAN, expiry date, deposit 00 B0 00 00 18
            apduCmd = new APDUCommand(0x00, 0xb0, 0, 0, null, 0x18);
            apdu1   = iCard.Transmit(apduCmd);
            //card number (CAN)
            Number = ConvertBCDCardNumber(apdu1.Data, 1, 8);
            //get the string form XXXX-XXXX-XXXX-XXXX
            StringBuilder sb = new StringBuilder();

            sb.Append(ConvertBCDCardNumber(apdu1.Data, 1, 2).ToString("D4"));
            sb.Append("-");
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 3, 2).ToString("D4"));
            sb.Append("-");
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 5, 2).ToString("D4"));
            sb.Append("-");
            sb.Append(ConvertBCDCardNumber(apdu1.Data, 7, 2).ToString("D4"));
            NumberString = sb.ToString();
            //read expiry date
            int year = apdu1.Data[12]; //year, from 1900 or 2000

            if (year > 90 && year < 100)
            {
                year += 1900;
            }
            else
            {
                year += 2000;
            }
            byte     month     = apdu1.Data[13];
            byte     validity  = (byte)ConvertBCDCardNumber(apdu1.Data, 14, 1); //validity in months
            DateTime issueDate = new DateTime(year, month, 1);

            Expiry       = issueDate.AddMonths(validity);
            ExpiryString = Expiry.ToString("dd MMM yyyy");
            Deposit      = ConvertBCDCardNumber(apdu1.Data, 15, 2) / 100.0f;

            //get transaction log
            //read number of total entries in log
            apduCmd = new APDUCommand(0, 0xb0, 0, 2, null, 4);
            apdu1   = iCard.Transmit(apduCmd);
            if (apdu1.SW1 != 0x90)
            {
                throw new Exception("Read NETS log size fail");
            }
            short logSize = (short)ConvertBCDCardNumber(new byte[] { apdu1.Data[2], apdu1.Data[3] }, 0, 2);

            apduCmd = new APDUCommand(0, 0xa4, 2, 0, new byte[] { 0, 5 }, 0);
            apdu1   = iCard.Transmit(apduCmd);
            if (apdu1.SW1 != 0x90)
            {
                throw new Exception("Read NETS transaction fail");
            }

            //read the current log entry number
            short endOffs = (short)(logSize * 4);
            byte  endP1   = (byte)((endOffs >> 8) & 0xff);
            byte  endP2   = (byte)(endOffs & 0xff);

            apduCmd = new APDUCommand(0, 0xb0, endP1, endP2, null, 2);
            apdu1   = iCard.Transmit(apduCmd);
            if (apdu1.SW1 != 0x90)
            {
                throw new Exception("Read NETS current log number fail");
            }
            short currentLogNum = (short)ConvertBCDCardNumber(new byte[] { apdu1.Data[0], apdu1.Data[1] }, 0, 2);

            currentLogNum -= 2;

            //read each log entry from the ring buffer, starting from current
            for (byte n = 0; n < logSize; n++)
            {
                int i = currentLogNum - n;
                if (i < 0)
                {
                    i += logSize;
                }
                short logOffs = (short)(i * 4);
                byte  logP1   = (byte)((logOffs >> 8) & 0xff);
                byte  logP2   = (byte)(logOffs & 0xff);
                //read binary 0xb0, offset logOffs, size 16
                apduCmd = new APDUCommand(0, 0xb0, logP1, logP2, null, 16);
                apdu1   = iCard.Transmit(apduCmd);
                if (apdu1.SW1 != 0x90)
                {
                    throw new Exception("Read NETS log entry fail");
                }
                NETSTransaction logEntry = new NETSTransaction(apdu1.Data);
                if (logEntry.Type != 0)
                {
                    TransactionLog.Add(logEntry);
                }
            }
        }