Change default key of rfid tag

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 <SPI.h>
#include <MFRC522.h>

#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
}

Tnx.

Perhaps you could help us by supplying a link to the documentation you are using!

I moved your topic to an appropriate forum category @clooner801.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Im using MFRC522 library as it's obvious in header of code! What kind of question is that

Thank you.
I can find no reference in your documentation relating to changing a default key.

im truly sorry about that. It's on my nerves that every code i tried to change the key didn't workout. It's been 2 weeks since im trying to change that key....
Do you have any idea?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.