NFC (2) NFC card for reading and writing mifareclassic Protocol

Source: Internet
Author: User
Tags nfca

Http://www.cnblogs.com/haoxinyue/archive/2012/05/03/2479599.html

Let's take a look at the mifareclassic protocol.

In the android SDK documentationMifareClassicI/O operations will be supported, andMIFARE_CLASSIC
Ndef tags will also be supported. In either case,NfcAWill also be enumerated on the tag, because all Mifare classic tags are alsoNfcA."
Therefore, the nfca protocol is compatible with the mifareclassic protocol. We can use the nfca class in Android to process the rfidcard assigned to mifareclassic.

Generally, the mifareclassic RF Card has three memory sizes:

1 K: 16 partitions (sector). Each partition has 4 blocks and each block has 16 bytes of data.

2 K: 32 partitions. Each partition has 4 blocks and each block has 16 bytes of data.

4 K: 64 partitions. Each partition has 4 blocks and each block has 16 bytes of data.

For all mifareclassic-based cards, the last block in each zone is trailer and 16 bytes are used to store the key in the read/write area. There can be two keys A and B, each key is 6 bytes long. The default key is generally FF or 0. The memory structure of the last block is as follows:

Block 0 Data 16 bytesblock 1 Data 16 bytesblock 2 Data 16 bytesblock 3 trailer 16 bytestrailer: Key A: 6 bytesaccess conditions: 4 byteskey B: 6 bytes so when writing the card's memory, generally, you cannot write the last block of each sector unless you need to modify the key and access permissions. If you accidentally modify key A, but you do not know what it is, you cannot access the corresponding sector. In mifareclassic, if you want to read data, you must have the permission of the sector where the data address is located. This permission is the Keya or key B of the trailer of the sector. Example of reading data:
// Tag is the tag obtained in onnewintent in the previous article.
MifareClassic mc = MifareClassic.get(tag);        short startAddress = 0;        short endAddress = 5;        byte[] data = new byte[(endAddress - startAddress + 1 ) * ByteCountPerBlock];                try {                        mc.connect();for (short i = startAddress; i <= endAddress; i++ ,time++) {                boolean auth = false;                short sectorAddress = getSectorAddress(i);                auth = mc.authenticateSectorWithKeyA(sectorAddress, MifareClassic.KEY_DEFAULT);                if (auth){                                        //the last block of the sector is used for KeyA and KeyB cannot be overwritted                    short readAddress = (short)(sectorAddress == 0 ? i : i + sectorAddress);                                        byte[] response = mc.readBlock(readAddress);                    CombineByteArray(data, response, time * ByteCountPerBlock);                }                else{                    throw new NfcException(NfcErrorCode.TemporaryError,                            "Authorization Error.");                }            }            mc.close();                    }        catch (NfcException ne) {            throw ne;        }        catch (IOException e) {            throw new NfcException(NfcErrorCode.TemporaryError,                    "Get response, what it is not successfully.", e);        }        finally        {            try {                mc.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }
Example of writing data:
// Tag is the tagmifareclassic MC = mifareclassic obtained in onnewintent in the previous article. get (TAG); try {MC. connect (); Boolean auth = false; short sectoraddress = 0 auth = MC. authenticatesectorwithkeya (sectoraddress, mifareclassic. key_default); If (auth) {// The last block of the sector is used for Keya and keyb cannot be overwritted MC. writeblock (readaddress, datatemp); MC. close () ;}finally {try {MC. close ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}

Download the complete sample code here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.