/// <summary>
        /// Authenticate access to a block
        /// </summary>
        /// <param name="keyAuth">Authorization type (Key A or Key B)</param>
        /// <param name="block">Block number</param>
        /// <param name="key">Key bytes value</param>
        /// <returns>Result of authentication</returns>
        public async Task <bool> Authenticate(MifareKeyAuth keyAuth, byte block, byte[] key)
        {
            byte[] dataOut = new byte[8 + id.Length]; // 8 -> fixed part (key type, block number, key bytes value)

            // key auth command (type A or B)
            dataOut[0] = (keyAuth == MifareKeyAuth.KeyA) ? MIFARE_AUTH_KEYA : MIFARE_AUTH_KEYB;
            // block number
            dataOut[1] = block;
            // key value
            Array.Copy(key, 0, dataOut, 2, key.Length);

            // tag ID
            for (int i = 8; i < dataOut.Length; i++)
            {
                dataOut[i] = id[i - 8];
            }

            byte[] dataIn = await reader.WriteRead(dataOut);

            if (dataIn == null)
            {
                return(false);
            }

            // check error code byte (pn532um.pdf, pag. 67)
            return(dataIn[0] == 0x00);
        }
Esempio n. 2
0
        /// <summary>
        /// Authenticate access to a block
        /// </summary>
        /// <param name="keyAuth">Authorization type (Key A or Key B)</param>
        /// <param name="block">Block number</param>
        /// <param name="key">Key bytes value</param>
        /// <returns>Result of authentication</returns>
        public bool Authenticate(MifareKeyAuth keyAuth, byte block, byte[] key)
        {
            byte[] dataOut = new byte[8 + id.Length]; // 8 -> fixed part (key type, block number, key bytes value)

            // key auth command (type A or B)
            dataOut[0] = (keyAuth == MifareKeyAuth.KeyA) ? MIFARE_AUTH_KEYA : MIFARE_AUTH_KEYB;
            // block number
            dataOut[1] = block;
            // key value
            Array.Copy(key, 0, dataOut, 2, key.Length);

            // tag ID
            for (int i = 8; i < dataOut.Length; i++)
                dataOut[i] = this.id[i - 8];

            byte[] dataIn = this.reader.WriteRead(dataOut);

            // check error code byte (pn532um.pdf, pag. 67)
            return (dataIn[0] == 0x00);
        }