Пример #1
0
        private static void DisplaySector(byte sector, Uid uid, byte[] key)
        {
            var buffer = _mfRc522.GetSector(uid, sector, key);

            if (uid.GetPiccType() == PiccType.Mifare1K)
            {
                var c = _mfRc522.GetAccessRights(buffer);
                Display1kBuffer(buffer, c);
            }
            else if (uid.GetPiccType() == PiccType.MifareUltralight)
            {
                DisplayUltralightBuffer(buffer);
            }
        }
Пример #2
0
        private static void DisplayUid(Uid uid)
        {
            string msg = "Uid of card is: ";

            for (int i = 0; i < (int)uid.UidType; i++)
            {
                msg += $"{uid.UidBytes[i]:X2} ";
            }
            msg += $"SAK: {uid.Sak:X2}";
            Debug.WriteLine(msg);
            switch (uid.GetPiccType())
            {
            case PiccType.Mifare1K:
                Debug.WriteLine("PICC type: MIFARE 1K");
                break;

            case PiccType.MifareUltralight:
                Debug.WriteLine("PICC type: MIFARE Ultralight");
                break;

            default:
                Debug.WriteLine("PICC type: Unknown");
                break;
            }
        }
Пример #3
0
        /// <summary>
        /// Read a page or a sector of a card
        /// </summary>
        /// <param name="uid"> uid of card to read</param>
        /// <param name="pageOrSector"> number of page or sector to read</param>
        /// <param name="key">key to authenticate (not used with Ultralight)</param>
        /// <param name="authenticateType">type of authentication (not used with Ultralight): A (default) or B</param>
        /// <returns></returns>
        public byte[][] GetSector(Uid uid, byte pageOrSector, byte[] key, PiccCommand authenticateType = PiccCommand.AuthenticateKeyA)
        {
            if (key == null || key.Length != 6)
            {
                throw new ArgumentException("Key must be a byte[] of length 6.", nameof(key));
            }
            switch (uid.GetPiccType())
            {
            case PiccType.Mifare1K:
                return(GetMifare1KSector(uid, pageOrSector, key, authenticateType));

            case PiccType.MifareUltralight:
                return(GetMifareUltraLight(pageOrSector));

            default:
                return(null);
            }
        }
Пример #4
0
        /// <summary>
        /// Write a page or a sector to a card
        /// </summary>
        /// <param name="uid"> uid of card to read</param>
        /// <param name="pageOrSector"> number of page or sector to read</param>
        /// <param name="key">key to authenticate (not used with Ultralight)</param>
        /// <param name="authenticateType">type of authentication (not used with Ultralight): A (default) or B</param>
        /// <returns></returns>
        public StatusCode PutSector(ArrayList dataBlock, Uid uid, byte pageOrSector, byte[] key, PiccCommand authenticateType = PiccCommand.AuthenticateKeyA)
        {
            InitData();

            if (key == null || key.Length != 6)
            {
                throw new ArgumentException("Key must be a byte[] of length 6.", nameof(key));
            }

            switch (uid.GetPiccType())
            {
            case PiccType.Mifare1K:

                if (pageOrSector > 15)
                {
                    throw new ArgumentOutOfRangeException(nameof(pageOrSector), "Sector must be between 0 and 15.");
                }

                for (int i = 0; i < 3; i++)
                {
                    if (dataBlock[i] != null)
                    {
                        var line = (string)dataBlock[i];
                        if (line.Length > 16)
                        {
                            throw new ArgumentException("dataBlock line must be 16 char. max lenght", nameof(line));
                        }

                        for (int j = 0; j < line.Length; j++)
                        {
                            data[i][j] = (byte)line[j];
                        }
                    }
                }
                return(PutMifare1KSector(uid, pageOrSector, key, data, authenticateType));

            case PiccType.MifareUltralight:

                if (pageOrSector < 4 || pageOrSector > 15)
                {
                    throw new ArgumentOutOfRangeException(nameof(pageOrSector), "Sector must be between 4 and 15.");
                }

                byte[] dataPage = new byte[4];
                if (dataBlock != null)
                {
                    var line = (string)dataBlock[0];
                    if (line.Length > 4)
                    {
                        throw new ArgumentException("dataBlock line must be 4 char. max lenght");
                    }
                    for (int j = 0; j < line.Length; j++)
                    {
                        dataPage[j] = (byte)line[j];
                    }
                }
                return(PutMifareUltraLight(pageOrSector, dataPage));

            default:
                return(StatusCode.Error);
            }
        }