Esempio n. 1
0
        private byte[][] GetMifare1KSector(Uid uid, byte sector, byte[] key, PiccCommand cmd = PiccCommand.AuthenticateKeyA)
        {
            if (sector > 15)
            {
                throw new ArgumentOutOfRangeException(nameof(sector), "Sector must be between 0 and 16.");
            }
            byte numberOfBlocks = 4;
            var  firstblock     = sector * numberOfBlocks;
            var  isTrailerBlock = true;

            byte[]   buffer       = new byte[18];
            byte[][] returnBuffer = new byte[4][];
            for (int i = 0; i < 4; i++)
            {
                returnBuffer[i] = new byte[16];
            }

            for (int i = numberOfBlocks - 1; i >= 0; i--)
            {
                var        blockAddr = (byte)(firstblock + i);
                StatusCode sc;
                if (isTrailerBlock)
                {
                    sc = Authenticate(uid, key, blockAddr, cmd);
                    if (sc != StatusCode.Ok)
                    {
                        throw new Exception($"Authenticate() failed:{sc}");
                    }
                }
                // Read block
                sc = MifareRead(blockAddr, buffer);
                if (sc != StatusCode.Ok)
                {
                    throw new Exception($"MifareRead() failed:{sc}");
                }
                if (isTrailerBlock)
                {
                    isTrailerBlock = false;
                }
                Array.Copy(buffer, returnBuffer[i], 16);
            }
            return(returnBuffer);
        }
Esempio n. 2
0
        private StatusCode PutMifare1KSector(Uid uid, byte sector, byte[] key, byte[][] data, PiccCommand cmd = PiccCommand.AuthenticateKeyA)
        {
            byte numberOfBlocks = 4;
            var  firstblock     = sector * numberOfBlocks;
            var  isTrailerBlock = true;

            StatusCode sc = StatusCode.Ok;

            for (int i = numberOfBlocks - 1; i >= 0; i--)
            {
                var blockAddr = (byte)(firstblock + i);
                if (isTrailerBlock)
                {
                    sc = Authenticate(uid, key, blockAddr, cmd);
                    if (sc != StatusCode.Ok)
                    {
                        throw new Exception($"Authenticate() failed:{sc}");
                    }
                }
                // Write block
                else
                {
                    sc = MifareWrite(blockAddr, data[i]);
                    if (sc != StatusCode.Ok)
                    {
                        throw new Exception($"MifareWrite() failed:{sc}");
                    }
                }
                if (isTrailerBlock)
                {
                    isTrailerBlock = false;
                }
            }
            return(sc);
        }
Esempio n. 3
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);
            }
        }