Hola, buenas tardes,
Estoy intentando programar el ESP para que cuando acerco una tarjeta cambie la clave de un sector si ese sector tiene la clave de fabrica:
hay una nueva tarjeta
....tiene la clave de fabrica
........cambiarla por la nueva clave
....no tiene la clave de fabrica
........verificar que sea la nueva clave
tengo dos versiones del código, el primero es este:
#include <SPI.h>
#include <MFRC522.h>
//------------------------------------------------------------------------------
//#### MODULO RFID ####
#define MFRC522_RST_PIN 0
#define MFRC522_SS_PIN 5
/********************************/
#define CLV_NVA_0 0xAA
#define CLV_NVA_1 0xBB
#define CLV_NVA_2 0xCC
#define CLV_NVA_3 0xDD
#define CLV_NVA_4 0xEE
#define CLV_NVA_5 0xFF
/********************************/
MFRC522 mfrc522(MFRC522_SS_PIN, MFRC522_RST_PIN);
byte finalDelSector[18] = {CLV_NVA_0, CLV_NVA_1, CLV_NVA_2, CLV_NVA_3,
CLV_NVA_4, CLV_NVA_5, /* 6 primeros nueva clave */
0xFF, 0x7, 0x80, 0x69, /* 4 siguientes FF, 7, 80, 69 */
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
}; /* 6 ultimos FF */
byte bloqueObjetivo = 31;
/******************************************************************************/
void setup()
{
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("Escribir datos personales en un PICC MIFARE "));
}
/******************************************************************************/
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent() ) return;
if ( ! mfrc522.PICC_ReadCardSerial() ) return;
//----------------------------------------------------------------------------
Serial.print(F("Tag ID:"));
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print(F(" Tipo Tag: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
Serial.println("");
Serial.println("");
//----------------------------------------------------------------------------
MFRC522::StatusCode estClvFabrica = tarjetaTieneClaveDeFabrica(bloqueObjetivo);
if ( estClvFabrica == MFRC522::STATUS_OK )
{
Serial.print("Cambiando clave de fabrica");
MFRC522::StatusCode estadoCambioClave = cambiarClaveDeFabrica(bloqueObjetivo);
if ( estadoCambioClave == MFRC522::STATUS_OK )
{
Serial.print(" **** Clave cambiada : ");
}
Serial.print(" ");
Serial.println(mfrc522.GetStatusCodeName(estadoCambioClave));
}
else
{
Serial.println("ERROR en la clave de fabrica : ");
Serial.println(mfrc522.GetStatusCodeName(estClvFabrica));
Serial.println("");
MFRC522::StatusCode estVerifClaveNueva;
Serial.print("**** Verificando clave nueva : ");
estVerifClaveNueva = verificarClaveNueva(bloqueObjetivo);
Serial.println(mfrc522.GetStatusCodeName(estVerifClaveNueva));
}
//----------------------------------------------------------------------------
Serial.println("");
Serial.println("");
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
/******************************************************************************/
MFRC522::StatusCode verificarClaveNueva(byte bloque)
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = finalDelSector[i];
/*
si key es la clave nueva
retorna MFRC522::STATUS_OK
si no
retorna el error correspondiente
*/
return mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
bloque, &key, &(mfrc522.uid));
}
/******************************************************************************/
MFRC522::StatusCode tarjetaTieneClaveDeFabrica(byte bloque)
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
/*
si key es la clave de fábrica ( FF FF FF FF FF FF )
retorna MFRC522::STATUS_OK para cambiarla por la clave nueva
*/
return mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
bloque, &key, &(mfrc522.uid));
}
/******************************************************************************/
MFRC522::StatusCode cambiarClaveDeFabrica(byte bloque)
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
MFRC522::StatusCode retCambioClave;
/* Serial.println(F("Autenticacion mediante key A...")); */
retCambioClave = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
bloque, &key, &(mfrc522.uid));
if (retCambioClave != MFRC522::STATUS_OK) return retCambioClave;
/* Escribir bloque */
retCambioClave = mfrc522.MIFARE_Write(bloque, finalDelSector, 16);
if (retCambioClave != MFRC522::STATUS_OK) return retCambioClave;
return retCambioClave;
}
que genera esta salida cuando paso una tarjeta con la clave de fabrica:
Escribir datos personales en un PICC MIFARE
Tag ID: 41 5D 23 1A Tipo Tag: MIFARE 1KBCambiando clave de fabrica Timeout in communication.
Tag ID: 41 5D 23 1A Tipo Tag: MIFARE 1KB
Cambiando clave de fabrica **** Clave cambiada : Success.
pero genera esta salida cuando paso una tarjeta con la clave nueva:
Escribir datos personales en un PICC MIFARE
Tag ID: 41 5D 23 1A Tipo Tag: MIFARE 1KBERROR en la clave de fabrica :
Timeout in communication.**** Verificando clave nueva : Timeout in communication.
Tag ID: 41 5D 23 1A Tipo Tag: MIFARE 1KB
y si le agrego un PCD_StopCrypto1 despues de cada autenticación como en este código:
#include <SPI.h>
#include <MFRC522.h>
//------------------------------------------------------------------------------
//#### MODULO RFID ####
#define MFRC522_RST_PIN 0
#define MFRC522_SS_PIN 5
/********************************/
#define CLV_NVA_0 0xAA
#define CLV_NVA_1 0xBB
#define CLV_NVA_2 0xCC
#define CLV_NVA_3 0xDD
#define CLV_NVA_4 0xEE
#define CLV_NVA_5 0xFF
/********************************/
MFRC522 mfrc522(MFRC522_SS_PIN, MFRC522_RST_PIN);
byte finalDelSector[18] = {CLV_NVA_0, CLV_NVA_1, CLV_NVA_2, CLV_NVA_3,
CLV_NVA_4, CLV_NVA_5, /* 6 primeros nueva clave */
0xFF, 0x7, 0x80, 0x69, /* 4 siguientes FF, 7, 80, 69 */
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
}; /* 6 ultimos FF */
byte bloqueObjetivo = 31;
/******************************************************************************/
void setup()
{
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("Escribir datos personales en un PICC MIFARE "));
}
/******************************************************************************/
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent() ) return;
if ( ! mfrc522.PICC_ReadCardSerial() ) return;
//----------------------------------------------------------------------------
Serial.print(F("Tag ID:"));
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print(F(" Tipo Tag: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
Serial.println("");
Serial.println("");
//----------------------------------------------------------------------------
MFRC522::StatusCode estClvFabrica = tarjetaTieneClaveDeFabrica(bloqueObjetivo);
if ( estClvFabrica == MFRC522::STATUS_OK )
{
Serial.print("Cambiando clave de fabrica");
MFRC522::StatusCode estadoCambioClave = cambiarClaveDeFabrica(bloqueObjetivo);
if ( estadoCambioClave == MFRC522::STATUS_OK )
{
Serial.print(" **** Clave cambiada : ");
}
Serial.print(" ");
Serial.println(mfrc522.GetStatusCodeName(estadoCambioClave));
}
else
{
Serial.println("ERROR en la clave de fabrica : ");
Serial.println(mfrc522.GetStatusCodeName(estClvFabrica));
Serial.println("");
MFRC522::StatusCode estVerifClaveNueva;
Serial.print("**** Verificando clave nueva : ");
estVerifClaveNueva = verificarClaveNueva(bloqueObjetivo);
Serial.println(mfrc522.GetStatusCodeName(estVerifClaveNueva));
}
//----------------------------------------------------------------------------
Serial.println("");
Serial.println("");
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
/******************************************************************************/
MFRC522::StatusCode verificarClaveNueva(byte bloque)
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = finalDelSector[i];
/*
si key es la clave nueva
retorna MFRC522::STATUS_OK
si no
retorna el error correspondiente
*/
return mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
bloque, &key, &(mfrc522.uid));
mfrc522.PCD_StopCrypto1();
}
/******************************************************************************/
MFRC522::StatusCode tarjetaTieneClaveDeFabrica(byte bloque)
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
/*
si key es la clave de fábrica ( FF FF FF FF FF FF )
retorna MFRC522::STATUS_OK para cambiarla por la clave nueva
*/
return mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
bloque, &key, &(mfrc522.uid));
mfrc522.PCD_StopCrypto1();
}
/******************************************************************************/
MFRC522::StatusCode cambiarClaveDeFabrica(byte bloque)
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
MFRC522::StatusCode retCambioClave;
/* Serial.println(F("Autenticacion mediante key A...")); */
retCambioClave = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
bloque, &key, &(mfrc522.uid));
if (retCambioClave != MFRC522::STATUS_OK) return retCambioClave;
/* Escribir bloque */
retCambioClave = mfrc522.MIFARE_Write(bloque, finalDelSector, 16);
if (retCambioClave != MFRC522::STATUS_OK) return retCambioClave;
mfrc522.PCD_StopCrypto1();
return retCambioClave;
}
me genera esta salida:
Escribir datos personales en un PICC MIFARE
Tag ID: 41 5D 23 1A Tipo Tag: MIFARE 1KBERROR en la clave de fabrica :
Timeout in communication.**** Verificando clave nueva : Timeout in communication.
¿me pueden ayudar con este tema?
muchas gracias,
saludos