Search code examples
androidnfcmifarendef

I can't read the same bytes from NFC cards


I am testing with Mifare-Ultralight nfc card. It is new and not have private key.

NFC Tools app and NXP TagInfo app, show me a difference in bytes read respect to my read data. result 1 and result 2. That why I suppose my code is wrong

my code:

byte[] key = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};

StringBuilder result = new StringBuilder();
for(int i=0;i<64;i+=4){
   if(util.authenticate(i, 0, key)){
      for(int j=i;j<i+4;j++){
         byte[] readData = util.read_block(i);
         if(readData != null){
            result.append(NfcUtil.toHexString(readData));
         }
      }
   }
}

All authenticate in blocks return false, even I tried all key in extended-std.keys

I tried commenting authenticate method, but then the result of data is different. I think the problem is here.

047BB94E8B700000FBA30000E1103E00277E047BB94E8B700000FBA30000E1103E00277E047BB94E8B700000FBA30000E1103E00277E047BB94E8B700000FBA30000E1103E00277E0312D1010E5402656E68656C6C6F20773EE40312D1010E5402656E68656C6C6F20773EE40312D1010E5402656E68656C6C6F20773EE40312D1010E5402656E68656C6C6F20773EE46F726C64FE0000000000000000000000CE626F726C64FE0000000000000000000000CE626F726C64FE0000000000000000000000CE626F726C64FE0000000000000000000000CE62000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749000000000000000000000000000000003749

If I decode that data the info is there, but no in standard Type2 Ndef format. Or at least not equal to NFC tools output.

Is necessary authentication for get right data all time?

I would have to authenticate in all the blocks even if I only have a record on the card?


Solution

  • Your looping over the blocks is wrong

    if you simplify the loop to just print out the blocks index you read

    for(int i=0;i<64;i+=4){
      for(int j=i;j<i+4;j++){
         System.out.print(i);
      }
    }
    
    

    gives a result of

    00004444888812121212161616162020202024242424282828283232323236363636404040404444444448484848525252525656565660606060

    So you are reading block 0 four times then block 4 four times, etc

    Which matches the repeating patterns in your data

    047BB94E8B700000FBA30000E1103E00277E047B

    As a read of a mifare ultralight will give you 4 blocks of 4 bytes from the start address of the read command your loop should look like

    for(int i=0;i<64;i+=4){
        if(util.authenticate(i, 0, key)){
            byte[] readData = util.read_block(i);
            if(readData != null){
                result.append(NfcUtil.toHexString(readData));
            }
        }
    }
    
    

    which means the start of the 4 block read is:-

    0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60

    (a read of block 0, returns data of blocks, 0,1,2,3 )

    I also note that a mifare Ultralight don't have multiple keys/passwords for different memory areas and once you authenticated once you can access the Tag until it is Halted. I know you are using a proprietary API to access these Tags but you should not need call util.authenticate more than once. But calling it multiple times won't have a bad affect.