Exemple #1
0
        internal Sector(MiFareCard card, int sector)
        {
            this.card   = card;
            this.sector = sector;

            dataBlocks = new DataBlock[NumDataBlocks];
            access     = null;
        }
        private void CardRemoved(object sender, EventArgs e)
        {
            Debug.WriteLine("Card Removed");
            card?.Dispose();
            card = null;

            ChangeTextBlockFontColor(TextBlock_Header, Colors.Red);
        }
        internal Sector(MiFareCard card, int sector)
        {
            this.card = card;
            this.sector = sector;

            dataBlocks = new DataBlock[NumDataBlocks];
            access = null;
        }
        public MiFareClassicSmartCard(string pin, string cardId, MiFareCard connection, int sector)
        {
            if (pin == null) throw new ArgumentNullException("pin");
            if (cardId == null) throw new ArgumentNullException("cardId");
            if (connection == null) throw new ArgumentNullException("connection");

            // add the key to the conection
            var userKey = PinToKeyBytes(pin, cardId);
            connection.AddOrUpdateSectorKeySet(new SectorKeySet
            {
                KeyType = KeyType.KeyA,
                Sector = sector,
                Key = userKey
            });

            this.connection = connection;
            this.sector = sector;
            CardId = cardId;
        }
        public MiFareClassicSmartCard(byte[] masterKey, string cardId, MiFareCard connection, int sector)
        {
            if (masterKey == null) throw new ArgumentNullException("masterKey");
            if (cardId == null) throw new ArgumentNullException("cardId");
            if (connection == null) throw new ArgumentNullException("connection");
            if(masterKey.Length != 6)
                throw new ArgumentOutOfRangeException("masterKey", "Key must be exactly 6 bytes");

            connection.AddOrUpdateSectorKeySet(new SectorKeySet
            {
                KeyType = KeyType.KeyB,
                Sector = sector,
                Key = masterKey
            });


            CardId = cardId;
            this.masterKey = masterKey;
            this.connection = connection;
            this.sector = sector;
        }
        /// <summary>
        /// Sample code to hande a couple of different cards based on the identification process
        /// </summary>
        /// <returns>None</returns>
        private async Task HandleCard(CardEventArgs args)
        {
            try
            {
                card?.Dispose();
                card = args.SmartCard.CreateMiFareCard();

                var localCard = card;

                var cardIdentification = await localCard.GetCardInfo();


                DisplayText("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString() + "\r\nCard name: " + cardIdentification.PcscCardName.ToString());

                if (cardIdentification.PcscDeviceClass == MiFare.PcSc.DeviceClass.StorageClass
                     && (cardIdentification.PcscCardName == CardName.MifareStandard1K || cardIdentification.PcscCardName == CardName.MifareStandard4K))
                {
                    // Handle MIFARE Standard/Classic
                    DisplayText("MIFARE Standard/Classic card detected");

                    var uid = await localCard.GetUid();
                    DisplayText("UID:  " + BitConverter.ToString(uid));

                    // 16 sectors, print out each one
                    for (var sector = 0; sector < 16 && card != null; sector++)
                    {
                        try
                        {
                            var data = await localCard.GetData(sector, 0, 48);

                            string hexString = "";
                            for (int i = 0; i < data.Length; i++)
                            {
                                hexString += data[i].ToString("X2") + " ";
                            }

                            DisplayText(string.Format("Sector '{0}':{1}", sector, hexString));

                        }
                        catch (Exception)
                        {
                            DisplayText("Failed to load sector: " + sector);
                        }
                    }

                }
            }
            catch (Exception e)
            {
                PopupMessage("HandleCard Exception: " + e.Message);
            }
        }
        private static async Task HandleCard(SmartCard args)
        {
            try
            {
                var newConnection = args.CreateMiFareCard();
                lock (cardConnectionLock)
                {
                    if (currentConnection != null)
                    {
                        currentConnection.Dispose();
                        currentCardId = null;
                        currentConnection = null;
                    }
                    currentConnection = newConnection;
                }

                var cardId = await currentConnection.GetCardInfo();
               
                Debug.WriteLine("Connected to card\r\nPC/SC device class: {0}\r\nCard name: {1}", cardId.PcscDeviceClass, cardId.PcscCardName);

                if (cardId.PcscDeviceClass == DeviceClass.StorageClass
                           && (cardId.PcscCardName == CardName.MifareStandard1K || cardId.PcscCardName == CardName.MifareStandard4K))
                {

                    Debug.WriteLine("MiFare Classic card detected");

                    var uid = await currentConnection.GetUid();
                    currentCardId = uid.ByteArrayToString();
                    
                    Debug.WriteLine("UID: " + currentCardId);

                }
                else
                {
                    throw new NotImplementedException("Card type is not implemented");
                }
                

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                
            }
        }
        private static void OnCardRemoved(SmartCardReader sender, CardRemovedEventArgs args)

        {
            lock (cardConnectionLock)
            {
                if (currentConnection != null)
                {
                    currentConnection.Dispose();
                    currentConnection = null;
                    currentCardId = null;
                }
            }


            // Let users know the card is gone
            // Raise on UI thread
            context.Post(_ =>
            {

                var evt = cardRemoved;
                if (evt != null)
                    evt(sender, EventArgs.Empty);
            }, null);
        }