0

I am using code for charge data in nfc tag and it's working if tag is already initialized and password protected. But I am facing problem to initialize an empty tag and I want to set Password to tag. How it can be done?

My code for authenticated tag is :

commandResponse = mifareUltralight.Transceive(getAuthenticateCmd());

Authenticate method is :

internal byte[] getAuthenticateCmd()
    {
        return new byte[5] {
            0x1b,
            password[0],
            password[1],
            password[2],
            password[3]
        };
    }

Where Password is :

internal byte[] password = new byte[4] { 0x39, 0x39, 0x39, 0x39 };

When I am trying this code to authenticate empty tag it throws TagLostException.

Srusti Thakkar
  • 1,835
  • 2
  • 22
  • 52
  • Is it not obvious I mean the whole concept of tagging is based on the premise that the tag that you assign is supposed to be non-empty. Can't you come up with something like a default tag if the tag is empty for some reason? – FreakyAli Feb 04 '20 at 06:38
  • Sorry, But I don't know anything about this. Can you please share some code? – Srusti Thakkar Feb 04 '20 at 06:44
  • How does your Tag become empty? – FreakyAli Feb 04 '20 at 06:48
  • Please share the Tag Model details as the setting password methods are specific to Tag type – Andrew Feb 04 '20 at 09:11
  • @FreakyAli, I got tags empty from Client. Don't know how. – Srusti Thakkar Feb 04 '20 at 09:55
  • @Andrew, Tag Model means? – Srusti Thakkar Feb 04 '20 at 09:57
  • You seem to be accessing it as a MifareUltraLight or compatible card but there are other card types. If you don't know the Tag's Model how can you be sure that this is the right way to access the card? I suggest using an App like https://play.google.com/store/apps/details?id=com.nxp.taginfolite&hl=en_US to try and determine the exact type of Tag you are working with and then you can get the datasheet to confirm you are access it correctly. – Andrew Feb 04 '20 at 10:19
  • @Andrew, NTAG213 – Srusti Thakkar Feb 04 '20 at 11:01
  • Getting commands wrong should not give you `TagLostException`, you should only get this when the card goes out of range while trying to interact with the card. Please show your card detection code and describe the process and timing of how you are moving the card in to range – Andrew Feb 04 '20 at 11:52

1 Answers1

0

first of all you should to check if the tag is protected by reading auth0, I would recommand this snip of code:

boolean isTagProtected() {
    MifareUltralight mu = MifareUltralight.get(tag);
    boolean passExist;
    try {
        mu.connect();
        byte[] answer = mu.readPages(227);
        byte auth0 = answer[3];
        String text = String.format("%02x", auth0);
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
        passExist = auth0 < (byte) 0xEB;
        mu.close();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "cannot make authentication", Toast.LENGTH_SHORT).show();
        passExist = true;
    }
    return passExist;
}

if the tag is not protected you could set new password by this code:

private static final byte[] SET_PACK_VALUE = {(byte) 0xA2, (byte) 0xE6, (byte) 0x11, (byte) 0x11, (byte) 0x00, (byte) 0x00};
private static final byte[] SET_PASSWORD = {(byte) 0xA2, (byte) 0xE5, (byte) 0x75, (byte) 0x6C, (byte) 0x66, (byte) 0x30};
private static final byte[] SET_AUTH0_VALUE = {(byte) 0xA2, (byte) 0xE3, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xA1};
if (isTagProtected()) {
      Toast.makeText(context, "The tag is already Locked", Toast.LENGTH_LONG).show();
 } else {

      NfcA nfcaTag = NfcA.get(maintag);
      try {
           nfcaTag.connect();
           nfcaTag.transceive(SET_PACK_VALUE);
           nfcaTag.transceive(SET_PASSWORD);
           nfcaTag.transceive(SET_AUTH0_VALUE);
           nfcaTag.close();
           Toast.makeText(context, "The tag is locked", Toast.LENGTH_LONG).show();
      } catch (IOException e) {
           Toast.makeText(context, "Error happend", Toast.LENGTH_LONG).show();
                            e.printStackTrace();
       }
   }

it the tag is protected you could auth it to be able to read and write on tag for just one session.

Here how to clear the password :

private final byte[] pwd_auth = new byte[]{(byte) 0x1b, 
//here write your pass (4 bytes)
};
public static final byte[] RESET_PACK_VALUE = {(byte) 0xA2, (byte) 0xE6, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
public static final byte[] RESET_AUTH0_VALUE = {(byte) 0xA2, (byte) 0xE3, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xFF};

MifareUltralight mfu = MifareUltralight.get(maintag);
try {
     byte[] answer;
     mfu.connect();

     answer = mfu.transceive(pwd_auth);
     mfu.transceive(MainActivity.RESET_AUTH0_VALUE);
     mfu.transceive(MainActivity.RESET_PACK_VALUE);
     mfu.close();
     Toast.makeText(context, "The Password is deleted", Toast.LENGTH_LONG).show();
     } catch (IOException e) {
           Toast.makeText(context, "Error happend , wrong pass", Toast.LENGTH_LONG).show();
           e.printStackTrace();
      }

in other words, you are trying to authenticate a tag wich dosen't have any password. you should to set a password and then authenicate it.

more general details: tag tech

here are more details about tag tech: the four parameters you need to edit some of them when you are dealing with password are :

PWD 32-bit password used for memory access protection. Reading PWD always returns 00000000h .

PACK 16-bit password acknowledge used during the password authentication process. Reading PACK always returns 0000h

AUTH0 Page address of Sector 0 from which onwards the password authentication is required to access the user memory from NFC perspective, dependent on NFC_PROT bit. If AUTH0 is set to a page address greater than EBh, the password protection is effectively disabled. Password protected area starts from page AUTH0 and ends at page EBh.

AUTHLIM no need to explain here.

P.S: I am using NT3H2111_2211 u should to read the link from Andrew in comments to know exactly where should you write PACK, PWD,and AUTH0.

Karam
  • 303
  • 2
  • 16
  • Here what is the password? Can you please share code for reset or format card? – Srusti Thakkar Feb 04 '20 at 10:48
  • I am getting every time true in "isTagProtected" method. – Srusti Thakkar Feb 04 '20 at 11:07
  • here I were just writing with A2h command and one byte for the page number . my password was `(byte) 0x75, (byte) 0x6C, (byte) 0x66, (byte) 0x30`. My code is really long you should to tell me which function you want me to share. – Karam Feb 04 '20 at 11:08
  • it your card is protected you need to know the password and authenticate the card the overwrite the old password, I will share the code how to delete the password and make the card opend, I guess you know the password of the tag? – Karam Feb 04 '20 at 11:21
  • As Srusti says the card is an NTAG 213 using the pages ranges E3h to E6h shown in your general details is wrong, that is for an NTAG 216. Instead use Pages 29h to 2Ch for NTAG213 (Full details https://www.nxp.com/docs/en/data-sheet/NTAG213_215_216.pdf ) – Andrew Feb 04 '20 at 11:29
  • @Karam, I know password. Can you please share code for delete password? – Srusti Thakkar Feb 04 '20 at 11:34
  • I don't know anything about NFC. I have just code for that and trying to understand that. But I am not getting actual what will be the process. – Srusti Thakkar Feb 04 '20 at 11:39
  • @SrustiThakkar I suggest you read in detail the datasheet for the NTAG213 I linked as this will give you an understanding of how to interact with the card. But on another point getting these commands wrong should not give you `TagLostException` – Andrew Feb 04 '20 at 11:49
  • @Andrew sure TagLostException could show with this command check this answer https://stackoverflow.com/questions/45502768/nfca-transceivebyte-data-throws-taglostexception – Karam Feb 04 '20 at 11:53
  • @Andrew, I had tried this code "https://stackoverflow.com/questions/45502768/nfca-transceivebyte-data-throws-taglostexception". First time it worked fine, but second time it throws error of Tag Lost sometime and sometime Transceive failed. – Srusti Thakkar Feb 04 '20 at 12:19
  • I don't think that the code in https://stackoverflow.com/questions/45502768/nfca-transceivebyte-data-throws-taglostexception is a good example of error checking as it does not try and catch the `IOException` that can also be generated and it does not correctly check for a `NACK` messages and then goes on use `transcieve` with no error checking at all. While there is a possibility of `TagLostException` this is not what the datasheet says should happen. This could be a bug in Android or the card itself. – Andrew Feb 04 '20 at 12:33
  • @Andrew, Okay, then what should I do? I am new to NFC and don't know when this exception will occurred. Can you share your code for initialize tag if empty with password protection and read or write data into that? – Srusti Thakkar Feb 04 '20 at 12:50
  • @SrustiThakkar did you try to delete the password with my code? – Karam Feb 04 '20 at 13:43
  • @Karam is mostly right but needs adjusting for your card `byte[] answer = mu.readPages(227);` should be `byte[] answer = mu.readPages(41);` for NTAG 213. For a Factory Initialised card the answer in byte 3 should be `FFh`. I currently don't have time to write a full answer but all `transcieve` (Read/Write/Other) commands should `catch` `TagLostException` and `IOException` as well as check the result is not a `NACK` which I do with `if ((answer.length == 1) && ((answer[0] & 0x00A) != 0x00A)) { // Not OK}` – Andrew Feb 04 '20 at 13:44
  • 1
    @SrustiThakkar try to read the accepted answer for this question https://stackoverflow.com/questions/40288795/android-nfca-connect-nfca-transceive-nfca-settimeout-and-nfca-getmaxtran/40303293#40303293 – Karam Feb 04 '20 at 13:50
  • @Karam, Yes tried code of Reset Password but not working. – Srusti Thakkar Feb 05 '20 at 03:58