Use android (android studio) and Apple ios mobile phone to read and write NFC card content (MifareUltralight) method to experience the process personally

Recently, I need to buy some nfc cards for reading and writing operations. The goal is to use mobile phones to achieve some business processes.

Taobao buys a card for 1-2 yuan, the information provided by the seller: the chip of the card is 215

The content found on the Internet is all paragraph by paragraph. I now integrate all the information I use so that I can know how to operate it after reading an article.

Android supports NFC phones, which are easier to use and more comfortable

Apple mobile phones are a bit special and have many restrictions. They are not as open as Android. The instructions for using NFC cards are as follows:

To read from an iPhone, you need iphone7+ ios12 or above to have the nfc function. When reading, you cannot read the card serial number directly. You need to install the NFC TagWriter by NXP_v4.8.2_apkpure.com.apk (downloadable from Google play) with an Android phone to write After the card, Apple can read the written content

 

1. The card belongs to the MifareUltralight format specification, which is as follows:

https://blog.csdn.net/wxh0000mm/article/details/79708807?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

There are two places here that are especially exclusive to add. The web articles on the Internet are not clearly explained, and I am a bit dizzy.

1. The serial number of the card is read in hexadecimal

2. The lock in page2 is marked with binary (and then converted to hexadecimal).

 

Storage structure:

Page number

Byte0

Byte1

Byte2

Byte3

Description

0

SN0

SN1

SN2

BCC0

Read only, store the serial number of the card: the first 3 bytes of Page0 + the entire Page1

Here is the hexadecimal reading (note the reading method, other web documents are not marked)

1

SN3

SN4

SN5

SN6

2

BCC1

Keep

LOCK0

LOCK1

Read only, 16 pages can be set to read only by setting LOCK0 and LOCK1

Binary mode setting (note the reading mode, other web documents are not marked)

3

OTP0

OTP1

OTP2

OTP3

Read and write, one-time transaction counter, irreversible

4

Data0

Data1

Data2

Data3

Read and write, data storage area

5

Data0

Data1

Data2

Data3

6

Data0

Data1

Data2

Data3

7

Data0

Data1

Data2

Data3

8

Data0

Data1

Data2

Data3

9

Data0

Data1

Data2

Data3

10

Data0

Data1

Data2

Data3

11

Data0

Data1

Data2

Data3

12

Data0

Data1

Data2

Data3

13

Data0

Data1

Data2

Data3

14

Data0

Data1

Data2

Data3

15

Data0

Data1

Data2

Data3

 

 The 3rd and 4th bytes of Page2 are used to lock the storage area as read-only. As shown in the figure below, if a bit of L4-L15 is set to 1, the content of the corresponding serial number is locked as read-only. Each page is Can be set individually. Lotp is used to lock Page3 as read-only. Lotp-L15 can lock others, these bits themselves are locked by three BL bits, BL15-10 is used to lock L15-L10, BL9-4 is used to lock L9-L4, and BLotp is used to lock Lotp. All these 16 lock positions also have OTP characteristics. In layman's terms, these "locks" do not have "keys", and once they are locked, they cannot be changed back, so be careful when locking.

 

For example, lock1 corresponds to the 8-bit binary 11110000, 1 means lock is read-only, and then the binary is converted to hexadecimal and written into the card

Binary mode setting (note the reading mode, other web documents are not marked)

2. There are many ways to read and write online, and the writing method is as follows:

https://www.cnblogs.com/sjjg/p/4783743.html

The pro-test is valid, my test source code download address: https://download.csdn.net/download/qq_16005627/12366636

Expand through the above read and write methods

1. How to read the card serial number

public String readTagc(Tag tag) throws Exception {
    //读数据 第1步,从nfc标签中得到MifareUltralight
    MifareUltralight ultralight = MifareUltralight.get(tag);

    try {
        //读数据 第2步,接连
        ultralight.connect();
        //读数据 第3步,从ultralight数据中的下标为4的位开始读数据.
        byte[] data = ultralight.readPages(0);
        byte[] serialNumber = new byte[7];
        serialNumber[0] = data[0];
        serialNumber[1] = data[1];
        serialNumber[2] = data[2];
        serialNumber[3] = data[4];
        serialNumber[4] = data[5];
        serialNumber[5] = data[6];
        serialNumber[6] = data[7];
        String CardCode = bytes2HexString(serialNumber);       
        return CardCode ;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            ultralight.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

 

//Hexadecimal to string

private static String bytes2HexString(final byte[] bytes) {
    if (bytes == null) return "";
    int len = bytes.length;
    if (len <= 0) return "";
    char[] ret = new char[len << 1];
    for (int i = 0, j = 0; i < len; i++) {
        ret[j++] = HEX_DIGITS[bytes[i] >> 4 & 0x0f];
        ret[j++] = HEX_DIGITS[bytes[i] & 0x0f];
    }
    return new String(ret);
}

private static final char[] HEX_DIGITS =
        {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

 

Second, set the lock in page2

ultralight.writePage(2, hexString2Bytes("DF48f000"));//第2页 LOCK设置     11110000二进制转16进制

//Hex to byte

public static byte[] hexString2Bytes(String src) 
    int l = src.length() / 2;
    byte[] ret = new byte[l];
    for (int i = 0; i < l; i++) {
        ret[i] = (byte) Integer
                .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
    }
    return ret;
}

Students who feel good, remember to like it!

 

Guess you like

Origin blog.csdn.net/qq_16005627/article/details/105786523