• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android NFCAdapter - Issue with AuthenticateSectorWithKeyA & Hex

BoolTech

Lurker
Developing Android App in .net Maui. Having issues with NFCAdapter, specifically passing a specific sector key with AuthenticateSectorWithKeyA.
We print and encode our own Mifare 1K cards. Print software allows us to change the sector key A. I have tried multiple, and all ok, but need a specific key to work which is failing authentication.

No problem passing the following Keys and authenticating the sector with AuthenticateSectorWithKeyA.

byte[] keybytes = new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff };
OR
byte[] keybytes = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF, (byte)0x12, (byte)0x34, (byte)0x56 };

var auth = tag.AuthenticateSectorWithKeyA(0, keybytes);

All ok, var auth = true

But when I try the following Key it fails to authenticate. var auth = false.

byte[] keybytes = new byte[] { (byte)0x19, (byte)0x23, (byte)0x86, (byte)0x18, (byte)0x05, (byte)0x00 };

HEXString is 192386180500

Only thing I can see if the last two bytes have a zero prefix


SO
Tried:

byte[] keybytes = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF, (byte)0x12, (byte)0x34, (byte)0x56 };
Result: OK - var auth = True

Tried:

byte[] keybytes = new byte[] { (byte)0x19, (byte)0x23, (byte)0x86, (byte)0x18, (byte)0x05, (byte)0x00 };

Result: Fail - var auth = false

Use this key with non android RFID readers and all is ok. Need to use this key as we have produced thousands of RFID cards with this key A for sector 0.

Bit new to Hex and byte arrays, any help welcome.
 
It's possible that the issue may be related to the way that the key is being converted to a byte array. Make sure that you are using the correct syntax to convert the hexadecimal string to a byte array.

Here is an example of how you can convert the hexadecimal string to a byte array:

Code:
string hexString = "192386180500";
byte[] keybytes = new byte[hexString.Length / 2];
for (int i = 0; i < keybytes.Length; i++)
{
  keybytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}

Alternatively, you can use the BitConverter.GetBytes method to convert the hexadecimal string to a byte array:

Code:
string hexString = "192386180500";
byte[] keybytes = BitConverter.GetBytes(long.Parse(hexString, System.Globalization.NumberStyles.HexNumber));

Once you have the keybytes array, you can pass it to the AuthenticateSectorWithKeyA method as usual.

It's also a good idea to double-check the value of the key that you are trying to use, as it's possible that there may be an issue with the key itself.

I hope this helps! Let me know if you have any questions.
 
Back
Top Bottom