Reauthenticate on RC522

Hello,

I was trying to change the keys and access bits on a sector and then reauthenticate on that sector. For some reason, it doesn't seem to work.
The current keyA is : { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }
The current keyB is : { 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB }
The accessBits are: { 0x08, ox77, 0x8F }
While the User Bit is set random (I think it's 92 at the moment).
The issue is that, if I'm authenticated using a key, and I try to authenticate using another, it fails.
Also, it fails to re-authenticate after a failed authentication. I'll post the code below with some comments:

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          53         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
MFRC522::MIFARE_Key key;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  if ( ! mfrc522.PCD_PerformSelfTest() )
    Serial.println("Self test failed");
  else Serial.println("Self test passed");
  mfrc522.PCD_Init();
  delay(4);       // Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
  // put your main code here, to run repeatedly:
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;
  for (byte i = 0; i < 6; i++)
    key.keyByte[i] = 0xAb;
  byte sectorTrailer = 15;
  MFRC522::StatusCode status;
  status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, sectorTrailer, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print("Failed to auth with key: "); dumpByteArray(key.keyByte, 6); Serial.println();
  } else {
    Serial.print("Auth good with: ");
    dumpByteArray(key.keyByte, 6);
    Serial.println();
  }

  mfrc522.PCD_StopCrypto1();
  mfrc522.PICC_HaltA();
  
  //mfrc522.PCD_Init();
  //mfrc522.PCD_StopCrypto1();
  
  for (byte i = 0; i < 6; i++)
    key.keyByte[i] = 0xAA;
  status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, sectorTrailer, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print("Failed to auth with key: "); dumpByteArray(key.keyByte, 6); Serial.println();
  } else {
    Serial.print("Auth good with: ");
    dumpByteArray(key.keyByte, 6);
    Serial.println();
  }
}
// Function used to dump an array of bytes
void dumpByteArray(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

So, after I try to authenticate with a wrong key, I can't get to auth with the right one, even after calling PCD_StopCrypto1() or PICC_HaltA();

Please shed some light no what's going on.

Thank you.

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