miguelbalboa / rfid

Arduino RFID Library for MFRC522
The Unlicense
2.73k stars 1.42k forks source link

Change default factory Key #627

Open clooner80 opened 2 months ago

clooner80 commented 2 months ago

Hi Im trying to change default key of a rfid tag from this FFFFFFFFFFFF To this 111111111111 using Arduino and Mfrc522 module. The problem is i get this error in serial monitor: -> Authentication failed: Timeout in communication. What should i do to be able to change this factory key? Dumpinfo example from the Mfrc522 library works perfectly, so it's not connections or wiring problem.

Here is my code: `#include

include

define RST_PIN 9 // Configurable, see typical pin layout above

define SS_PIN 10 // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

// Define the sector trailer block where the access bits are stored

define TRAILER_BLOCK 3

void setup() { Serial.begin(9600); // Initialize serial communications with the PC SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 card Serial.println(F("Change access bits and authentication key for a MIFARE PICC ")); }

void loop() { // Prepare the current and new keys MFRC522::MIFARE_Key currentKey; MFRC522::MIFARE_Key newKey; for (byte i = 0; i < 6; i++) { currentKey.keyByte[i] = 0xFF; // Default key newKey.keyByte[i] = 0x11; // New key (change this to your desired key) }

// Reset the loop if no new card present on the sensor/reader if (!mfrc522.PICC_IsNewCardPresent()) { return; }

// Select one of the cards if (!mfrc522.PICC_ReadCardSerial()) { return; }

// Authenticate with the current key byte block = 1; // Example block to authenticate MFRC522::StatusCode status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &currentKey, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("Authentication failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; }

// Change authentication key status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &newKey, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("Key change failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; }

// Inform about successful key change Serial.println(F("Access bits and authentication key changed successfully"));

mfrc522.PICC_HaltA(); // Halt PICC mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD }`