示例#1
0
        private byte[] invokeSmartcard()
        {
            //i blanked the commands, and parameters - i am not sure if i am allowed to publish them
            APDUCommand  apduEcard1 = new APDUCommand(0x00, 0x00, 04, 0x00, null, 0);
            APDUCommand  apduEcard2 = new APDUCommand(0x00, 0x00, 0, 0, null, 0);
            APDUCommand  apduEcard3 = new APDUCommand(0x00, 0x00, 00, 0x00, null, 255);
            APDUResponse apduResp;

            CardNative iCard = new CardNative();

            iCard.Connect(reader, SHARE.Shared, PROTOCOL.T0orT1); //connected
            // Ecard1
            byte[]    data1     = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            APDUParam apduParam = new APDUParam();

            apduParam.Data = data1;
            apduEcard1.Update(apduParam);
            apduResp = iCard.Transmit(apduEcard1);
            // Ecard2
            apduParam.Data = new byte[] { 0x00, 0x00 };
            apduEcard2     = new APDUCommand(0x00, 0x00, 00, 0x0C, apduParam.Data, 0);
            apduEcard2.Update(apduParam);
            apduResp = iCard.Transmit(apduEcard2);
            // Ecard3
            apduParam.Data = new byte[] { 0 };
            apduEcard3     = new APDUCommand(0x00, 0x00, 00, 0x00, null, 255);
            apduResp       = iCard.Transmit(apduEcard3);
            return(apduResp.Data);
        }
        /// <summary>
        /// Mains the specified arguments.
        /// </summary>
        /// <param name="args">The arguments.</param>
        static void Main(string[] args)
        {
            using (CardNative card = new CardNative())
            {
                // Obter a lista de leitores.

                string[] leitores =
                    card.ListReaders();


                if (leitores != null && leitores.Length > 0)
                {
                    // Pede ao utilizador para escolher o leitor preferido.

                    bool leitorInvalido = true;

                    int numero = -1;

                    do
                    {
                        Console.WriteLine("Escolha o leitor:");

                        for (int i = 0; i < leitores.Length; i++)
                        {
                            Console.WriteLine(i + "---> " + leitores[i]);
                        }

                        string input = Console.ReadLine();


                        if (!Int32.TryParse(input, out numero) || numero < 0 || numero >= leitores.Length)
                        {
                            Console.WriteLine("O número de leitor é inválido");
                            Console.WriteLine(Environment.NewLine);
                        }
                        else
                        {
                            leitorInvalido = false;
                        }
                    } while (leitorInvalido);

                    Console.WriteLine("Abra agora a página index.html para ver o resultado final");

                    AsyncServer asyncServer = new AsyncServer(leitores[numero]);

                    Console.WriteLine("Prima uma tecla para sair.");
                    Console.Read();
                }
                else
                {
                    Console.WriteLine("Nenhum leitor de cartão de cidadão encontrado.");
                    Console.WriteLine("Prima uma tecla para sair.");
                    Console.Read();
                }

                card.Dispose();
            }
        }
示例#3
0
 public ACR122_v2()
 {
     _connected = false;
     try{
         iCard = new CardNative();
     }catch (System.Exception) {
         Debug.WriteLine("ACR112_v2:: CardNative() Failed");
     }
 }
        public CardAuthenticationService()
        {
            _native  = new CardNative();
            _inserts =
                Observable.FromEventPattern <EventArgs>(_native, "OnCardInserted")
                .Select(_ => new Unit());

            _removes =
                Observable.FromEventPattern <EventArgs>(_native, "OnCardRemoved")
                .Select(_ => new Unit());
        }
示例#5
0
        public bool connect(string device)
        {
            iCard = new CardNative();

            String[] readers;

            try{
                readers = this.iCard.ListReaders();
            }catch (System.Exception) {
                Debug.WriteLine("ACR122:: connect, ListReaders() failed");
                return(false);
            }

            if (readers == null)
            {
                Debug.WriteLine("ARC122:: No Devices connected!");
                return(false);
            }

            bool ready = false;

            while (!ready)
            {
                try{
                    this.iCard.Connect(device, SHARE.Shared, PROTOCOL.T0orT1);
                    _connected = true;
                    ready      = true;
                }catch (System.Exception e) {
                    //Debug.WriteLine("ACR112:: ERROR, " + e.Message);
                    Debug.WriteLine("Device not ready");
                    System.Threading.Thread.Sleep(1500);
                }
            }

            Debug.WriteLine("ACR122:: Device Connected!");
            return(true);
        }
示例#6
0
        static void Main(string[] args)
        {
            try
            {
                int    nbRecords = 10;
                string PIN       = "31323334FFFFFFFF";
                bool   bPin      = false;

                if (args.Length != 0)
                {
                    for (int nI = 0; nI < args.Length; nI++)
                    {
                        if (args[nI] == "P")
                        {
                            bPin = true;
                            PIN  = FormatPIN(args[++nI]);
                        }
                        else
                        {
                            nbRecords = int.Parse(args[nI]);
                        }
                    }
                }

                APDUResponse apduResp = null;
                CardNative   iCard    = new CardNative();

                string[] readers = iCard.ListReaders();

                iCard.Connect(readers[0], SHARE.Shared, PROTOCOL.T0orT1);

                APDUPlayer player = new APDUPlayer(iCard);
                player.LoadAPDUFile(Properties.Settings.Default.DataPath + ApduFile);
                player.LoadSequenceFile(Properties.Settings.Default.DataPath + SequenceFile);

                SequenceParameter seqParam = new SequenceParameter();

                // Process Apdu: VerifyCHV
                if (bPin)
                {
                    Console.WriteLine("Sequence: Verify CHV1");
                    seqParam.Add("PIN", PIN);
                    apduResp = player.ProcessSequence("Verify CHV1", seqParam);
                    Console.WriteLine(apduResp.ToString());
                }

                if (!bPin || (bPin && apduResp.Status == 0x9000))
                {
                    for (int nI = 1; nI <= nbRecords; nI++)
                    {
                        seqParam.Clear();
                        seqParam.Add("Record", nI.ToString());
                        //Console.WriteLine(string.Format("Read ADN, Record={0}", nI));
                        apduResp = player.ProcessSequence("Read ADN", seqParam);

                        PhoneNumber phone = new PhoneNumber(apduResp.Data);
                        Console.WriteLine("ADN n°" + nI.ToString());
                        Console.WriteLine(phone.ToString());
                        Console.WriteLine();

                        //Console.WriteLine(apduResp.ToString());
                    }
                }

                APDULogList log = player.Log;

                Console.WriteLine("Log:");
                APDULog[] arrayLog = log.ToArray();
                for (int nI = 0; nI < log.Count; nI++)
                {
                    Console.WriteLine(arrayLog[nI].ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
示例#7
0
 public ACR122Query_v2(CardNative iCard)
 {
     this.iCard = iCard;
     _numCards  = -1;
     select();
 }
示例#8
0
        /// <summary>
        /// This program tests the API with a SIM card.
        /// If your PIN is activated be carefull when presenting the PIN to your card!
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                APDUResponse apduResp;

                CardNative iCard = new CardNative();

                string[] readers = iCard.ListReaders();

                iCard.Connect(readers[0], SHARE.Shared, PROTOCOL.T0orT1);
                Console.WriteLine("Connects card on reader: " + readers[0]);

                // Verify the PIN (if necessary)
                byte[]    pin       = new byte[] { 0x31, 0x32, 0x33, 0x34, 0xFF, 0xFF, 0xFF, 0xFF };
                APDUParam apduParam = new APDUParam();
                apduParam.Data = pin;
                apduVerifyCHV.Update(apduParam);
                //apduResp = iCard.Transmit(apduVerifyCHV);

                // Select the MF (3F00)
                apduParam.Data = new byte[] { 0x3F, 0x00 };
                apduSelectFile.Update(apduParam);
                apduResp = iCard.Transmit(apduSelectFile);
                if (apduResp.Status != SC_OK && apduResp.SW1 != SC_PENDING)
                {
                    throw new Exception("Select command failed: " + apduResp.ToString());
                }
                Console.WriteLine("MF selected");

                // Select the EFtelecom (7F10)
                apduParam.Data = new byte[] { 0x7F, 0x10 };
                apduSelectFile.Update(apduParam);
                apduResp = iCard.Transmit(apduSelectFile);
                if (apduResp.Status != SC_OK && apduResp.SW1 != SC_PENDING)
                {
                    throw new Exception("Select command failed: " + apduResp.ToString());
                }
                Console.WriteLine("DFtelecom selected");

                // Select the EFadn (6F3A)
                apduParam.Data = new byte[] { 0x6F, 0x3A };
                apduSelectFile.Update(apduParam);
                apduResp = iCard.Transmit(apduSelectFile);
                if (apduResp.Status != SC_OK && apduResp.SW1 != SC_PENDING)
                {
                    throw new Exception("Select command failed: " + apduResp.ToString());
                }
                Console.WriteLine("EFadn (Phone numbers) selected");

                // Read the response
                if (apduResp.SW1 == SC_PENDING)
                {
                    apduParam.Reset();
                    apduParam.Le   = apduResp.SW2;
                    apduParam.Data = null;
                    apduGetResponse.Update(apduParam);
                    apduResp = iCard.Transmit(apduGetResponse);
                    if (apduResp.Status != SC_OK)
                    {
                        throw new Exception("Select command failed: " + apduResp.ToString());
                    }
                }

                // Get the length of the record
                int recordLength = apduResp.Data[14];

                Console.WriteLine("Reading the Phone number 10 first entries");
                // Read the 10 first record of the file
                for (int nI = 0; nI < 10; nI++)
                {
                    apduParam.Reset();
                    apduParam.Le = (byte)recordLength;
                    apduParam.P1 = (byte)(nI + 1);
                    apduReadRecord.Update(apduParam);
                    apduResp = iCard.Transmit(apduReadRecord);

                    if (apduResp.Status != SC_OK)
                    {
                        throw new Exception("ReadRecord command failed: " + apduResp.ToString());
                    }

                    Console.WriteLine("Record #" + ((int)(nI + 1)).ToString());
                    Console.WriteLine(apduResp.ToString());
                }

                iCard.Disconnect(DISCONNECT.Unpower);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
示例#9
0
        /// <summary>
        /// Reads personal records from the currently inserted ID-card into the <tt>records</tt> array.
        /// </summary>
        private static void readRecords()
        {
            CardNative   iCard     = new CardNative();
            APDUParam    apduParam = new APDUParam();
            APDUResponse apduResp;

            string[] readers = iCard.ListReaders();

            /* Change this index to the one where your card is inserted. This is usually 0,
             * unless you have multiple readers. */
            string reader = readers[0];

            iCard.Connect(reader, SHARE.Shared, PROTOCOL.T0);
            Console.Write("Reading personal data from " + reader + "...");

            try
            {
                /* Select the Master File */
                apduResp = iCard.Transmit(apduSelectMF);
                if (apduResp.Status != SC_OK)
                {
                    throw new Exception("Select MF failed: " + apduResp.ToString());
                }

                /* Select EEEE */
                apduResp = iCard.Transmit(apduSelectEEEE);
                if (apduResp.Status != SC_OK)
                {
                    throw new Exception("Select EEEE failed: " + apduResp.ToString());
                }

                /* Select 5044 */
                apduResp = iCard.Transmit(apduSelect5044);
                if (apduResp.Status != SC_OK)
                {
                    throw new Exception("Select 5044 failed: " + apduResp.ToString());
                }

                /* Read the records */
                for (byte i = 1; i <= NUM_RECORDS; i++)
                {
                    /* Send the read command */
                    apduParam.Reset();
                    apduParam.P1 = i;
                    apduReadRecord.Update(apduParam);
                    apduResp = iCard.Transmit(apduReadRecord);
                    if (apduResp.SW1 != SC_PENDING)
                    {
                        throw new Exception("Read record failed: " + apduResp.ToString());
                    }

                    /* Read the response */
                    apduParam.Reset();
                    apduParam.Le = apduResp.SW2;
                    apduGetResponse.Update(apduParam);
                    apduResp = iCard.Transmit(apduGetResponse);
                    if (apduResp.Status != SC_OK)
                    {
                        throw new Exception("Get response failed: " + apduResp.ToString());
                    }
                    records[i - 1] = Encoding.UTF7.GetString(apduResp.Data).Trim();
                }
            }
            finally
            {
                iCard.Disconnect(DISCONNECT.Leave);
            }
        }