public async Task SetUserPin(string pin, string data)
        {
            if (pin == null)
            {
                throw new ArgumentNullException("pin");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            await Task.Run(async() =>
            {
                try
                {
                    var s = connection.GetSector(sector);

                    var dataBytes = Encoding.UTF8.GetBytes(data);

                    if (dataBytes.Length > s.DataLength)
                    {
                        throw new ArgumentOutOfRangeException("data",
                                                              string.Format(
                                                                  "Data is too long, must be shorter than '{0}' bytes. Current length is '{1}'",
                                                                  s.DataLength, dataBytes.Length));
                    }


                    // new array with data length
                    var sectorData = new byte[s.DataLength];
                    Array.Copy(dataBytes, sectorData, dataBytes.Length);

                    // Set the username
                    await s.SetData(sectorData, 0);
                    await s.Flush();

                    // set the pin
                    var keyBytes = PinToKeyBytes(pin, CardId);

                    Debug.WriteLine("Setting Pin bytes '{0}' ", keyBytes.ByteArrayToString());

                    await s.FlushTrailer(keyBytes.ByteArrayToString(), masterKey.ByteArrayToString());

                    // Update the stored key for key a
                    connection.AddOrUpdateSectorKeySet(new SectorKeySet
                    {
                        KeyType = KeyType.KeyA,
                        Sector  = sector,
                        Key     = keyBytes
                    });
                }
                catch (Exception e)
                {
                    throw new SmartCardException("Could not authenticate to card", e);
                }
            });
        }
        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;
        }
        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;
        }
Exemple #4
0
 private static void SetKeys()
 {
     if (_keys != null)
     {
         foreach (var key in _keys)
         {
             card.AddOrUpdateSectorKeySet(key);
         }
     }
     else
     {
         throw new Exception("Empty keys list");
     }
 }
Exemple #5
0
        public static int Authentication(IntPtr obj, int sector, int keyTypeInt, bool nonvolatileMemory, int keyIndex)
        {
            string text =
                $"!!! Authentication !!!\tobj:{obj}\tSector:{sector}\tKeyType:{keyTypeInt}\tNonvolatileMemory:{nonvolatileMemory}\tKeyIndex:{keyIndex}\t";

            WriteToLog(text);
            try
            {
                if (_reader == null || _card == null)
                {
                    WriteToLog($"!!! Authentication ERROR _reader:{_reader == null} _card:{_card == null}");
                    return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
                }
                var keyA = GetKeyFromCollection(sector, 0);
                var keyB = GetKeyFromCollection(sector, 1);

                if (/*KeyType_int == 0 &&*/ keyA != null)
                {
                    _card.AddOrUpdateSectorKeySet(new SectorKeySet {
                        KeyType = KeyType.KeyA, Sector = sector, Key = keyA
                    });
                }
                if (/*KeyType_int == 1 &&*/ keyB != null)
                {
                    _card.AddOrUpdateSectorKeySet(new SectorKeySet {
                        KeyType = KeyType.KeyB, Sector = sector, Key = keyB
                    });
                }

/*
 *              var sec = _card.GetSector(Sector);
 *
 *              //TODO только для тестов!
 *              if (_cardBadSectors?.Contains(new Tuple<int, int>(Sector, sec.NumDataBlocks -  1))??false)
 *                  return (int) ErrorCodes.E_SUCCESS;
 *
 *              var secAuthentification = sec.GetData(0).Result;
 *              if (secAuthentification == null)
 *                  return (int)ErrorCodes.E_CARDREADER_NOT_INIT;
 */
                return((int)ErrorCodes.E_SUCCESS);
            }
            catch (Exception e)
            {
                WriteToLog($"Authentication ERROR !!! reader:{_reader} card: {_card}\r\n {text}\r\n {e}");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
        }