changeAuthKey method
Updates the authentication key on a Mifare Ultralight C RFID tag.
Changes the RFID tag's authentication key to a new 16-byte key. This key is split and written to specific memory blocks in a reversed order to enhance security.
Parameters
key
: A 16-byte list representing the new authentication key. Each byte must be within the range 0x00 to 0xFF.
Throws
Exception
for invalid key length if the key is not exactly 16 bytes.Exception
if any byte in the key is outside the valid range.Exception
labeled 'Error changing authentication key' upon failure to write the new key to the tag's memory.
Example
try {
await card.changeAuthKey(key: [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]);
print('Authentication key updated successfully.');
} catch (e) {
print('Failed to update authentication key: $e');
}
Note: It's crucial to handle the new authentication key securely and ensure its confidentiality to maintain the tag's security.
Implementation
Future<void> changeAuthKey({required List<int> key}) async {
if (key.length != 16) {
throw Exception('Invalid key length');
}
try {
validateByteList(key);
} catch (e) {
throw Exception('Invalid key. Each byte must be within 0x00 and 0xFF');
}
final key1 = key.sublist(0, 8);
final key2 = key.sublist(8, 16);
try {
await reader.writeBlock(
blockNumber: AUTH_KEY_ADDRESS_START,
data: key1.reversed.toList().sublist(0, 4),
);
await reader.writeBlock(
blockNumber: AUTH_KEY_ADDRESS_START + 1,
data: key1.reversed.toList().sublist(4, 8),
);
await reader.writeBlock(
blockNumber: AUTH_KEY_ADDRESS_START + 2,
data: key2.reversed.toList().sublist(0, 4),
);
await reader.writeBlock(
blockNumber: AUTH_KEY_ADDRESS_START + 3,
data: key2.reversed.toList().sublist(4, 8),
);
} catch (e) {
throw Exception('Error changing authentication key');
}
}