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;
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        private void CardRemoved(object sender, EventArgs e)
        {
            Debug.WriteLine("Card Removed");
            card?.Dispose();
            card = null;

            ChangeTextBlockFontColor(TextBlock_Header, Windows.UI.Colors.Red);
        }
Exemple #5
0
        /// <summary>
        /// Sample code to hande a couple of different cards based on the identification process
        /// </summary>
        /// <returns>None</returns>
        private async Task HandleCard(CardAddedEventArgs args)
        {
            try
            {
                card?.Dispose();
                card = args.SmartCard.CreateMiFareCard();



                var cardIdentification = await card.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 card.GetUid();

                    DisplayText("UID:  " + BitConverter.ToString(uid));



                    // 16 sectors, print out each one
                    for (var sector = 0; sector < 16; sector++)
                    {
                        try
                        {
                            var data = await card.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);
            }
        }
Exemple #6
0
        private async void CardAdded(object sender, CardEventArgs args)
        {
            cardEventArg = args;
            lblCardPresence.Invoke(new Action(() => lblCardPresence.Text = "Card Present"));
            MiFareCard card = args.SmartCard.CreateMiFareCard();

            var cardIdentification = await card.GetCardInfo();

            var uid = await card.GetUid();

            lblCardInfo.Invoke(new Action(() => lblCardInfo.Text = "Card Info \n Device Class: " + cardIdentification.PcscDeviceClass.ToString() + "\n Card Name: " + cardIdentification.PcscCardName.ToString() + "\n Card UID: " + BitConverter.ToString(uid)));
        }
Exemple #7
0
 private static void Reader_CardAdded(object sender, CardAddedEventArgs ev)
 {
     WriteToLog("Reader_CardAdded");
     try
     {
         _card?.Dispose();
         _card           = ev.SmartCard.CreateMiFareCard();
         _cardBadSectors = ReadBadsFromFileSector();
     }
     catch (Exception e)
     {
         WriteToLog($"Reader_CardAdded ERROR!!!\r\n {e}");
         throw;
     }
 }
Exemple #8
0
 private static void Reader_CardRemoved(object sender, CardRemovedEventArgs ev)
 {
     WriteToLog("Reader_CardRemoved");
     try
     {
         _card?.Dispose();
         _card = null;
         _cardBadSectors?.Clear();
         _keys?.Clear();
     }
     catch (Exception e)
     {
         WriteToLog($"Reader_CardRemoved ERROR!!!\r\n {e}");
         throw;
     }
 }
Exemple #9
0
        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);
            }
        }
Exemple #10
0
 private async Task <byte[]> loadBlock(int sector, int block, MiFareCard card)
 {
     try
     {
         if (block < 3)
         {
             return(await card.GetData(sector, block, 16));
         }
         if (block == 3)
         {
             return(await card.GetData(sector, 3, 16));
         }
         if (block == 5)
         {
             return(await card.GetData(sector, 3, 16));
         }
     }
     catch
     {
         return(null);
     }
     return(null);
 }
Exemple #11
0
        private async void loadSector(object sender, EventArgs e)
        {
            int sector = cmbSector.SelectedIndex;

            IList <SectorKeySet> authenticationKeys = new List <SectorKeySet>();

            authenticationKeys.Add(new SectorKeySet());
            authenticationKeys.Last().KeyType = MiFare.Classic.KeyType.KeyA;
            authenticationKeys.Last().Key     = Extensions.StringToByteArray(txtKeyALoad.Text);
            authenticationKeys.Last().Sector  = sector;
            authenticationKeys.Add(new SectorKeySet());
            authenticationKeys.Last().KeyType = MiFare.Classic.KeyType.KeyB;
            authenticationKeys.Last().Key     = Extensions.StringToByteArray(txtKeyBLoad.Text);
            authenticationKeys.Last().Sector  = sector;

            MiFareCard card = cardEventArg.SmartCard.CreateMiFareCard(authenticationKeys);

            byte[] blk0 = await loadBlock(sector, 0, card);

            if (blk0 == null)
            {
                txtBlk0.Text = "Block read access restricted";
            }
            else
            {
                txtBlk0.Text = Extensions.ByteArrayToString(blk0);
            }

            byte[] blk1 = await loadBlock(sector, 1, card);

            if (blk1 == null)
            {
                txtBlk1.Text = "Block read access restricted";
            }
            else
            {
                txtBlk1.Text = Extensions.ByteArrayToString(blk1);
            }

            byte[] blk2 = await loadBlock(sector, 2, card);

            if (blk2 == null)
            {
                txtBlk2.Text = "Block read access restricted";
            }
            else
            {
                txtBlk2.Text = Extensions.ByteArrayToString(blk2);
            }

            byte[] keyA = await loadBlock(sector, 3, card);

            if (keyA == null)
            {
                txtKeyA.Text = "Block read access restricted";
            }
            else
            {
                txtKeyA.Text = Extensions.ByteArrayToString(keyA.Skip(10).Take(6).ToArray());
            }

            byte[] keyB = await loadBlock(sector, 5, card);

            if (keyB == null)
            {
                txtKeyB.Text = "Block read access restricted";
            }
            else
            {
                txtKeyB.Text = Extensions.ByteArrayToString(keyB.Take(6).ToArray());
            }
        }
Exemple #12
0
 private static void Reader_CardAdded(SmartCardReader sender, CardAddedEventArgs args)
 {
     card = args.SmartCard.CreateMiFareCard();
     RaiseStatusUpdate(true);
 }
Exemple #13
0
 private static void Reader_CardRemoved(SmartCardReader sender, CardRemovedEventArgs args)
 {
     card = null;
     RaiseStatusUpdate(false);
 }