rfid_default_keys.ino 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
  4. * for further details and other examples.
  5. *
  6. * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
  7. *
  8. * Released into the public domain.
  9. * ----------------------------------------------------------------------------
  10. * Example sketch/program which will try the most used default keys listed in
  11. * https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys to dump the
  12. * block 0 of a MIFARE RFID card using a RFID-RC522 reader.
  13. *
  14. * Typical pin layout used:
  15. * -----------------------------------------------------------------------------------------
  16. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  17. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  18. * Signal Pin Pin Pin Pin Pin Pin
  19. * -----------------------------------------------------------------------------------------
  20. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  21. * SPI SS SDA(SS) 10 53 D10 10 10
  22. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  23. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  24. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  25. *
  26. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  27. *
  28. */
  29. #include <SPI.h>
  30. #include <MFRC522.h>
  31. #define RST_PIN 9 // Configurable, see typical pin layout above
  32. #define SS_PIN 10 // Configurable, see typical pin layout above
  33. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  34. // Number of known default keys (hard-coded)
  35. // NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
  36. #define NR_KNOWN_KEYS 8
  37. // Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
  38. byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
  39. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
  40. {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
  41. {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
  42. {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
  43. {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
  44. {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
  45. {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
  46. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
  47. };
  48. /*
  49. * Initialize.
  50. */
  51. void setup() {
  52. Serial.begin(9600); // Initialize serial communications with the PC
  53. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  54. SPI.begin(); // Init SPI bus
  55. mfrc522.PCD_Init(); // Init MFRC522 card
  56. Serial.println(F("Try the most used default keys to print block 0 of a MIFARE PICC."));
  57. }
  58. /*
  59. * Helper routine to dump a byte array as hex values to Serial.
  60. */
  61. void dump_byte_array(byte *buffer, byte bufferSize) {
  62. for (byte i = 0; i < bufferSize; i++) {
  63. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  64. Serial.print(buffer[i], HEX);
  65. }
  66. }
  67. /*
  68. * Try using the PICC (the tag/card) with the given key to access block 0.
  69. * On success, it will show the key details, and dump the block data on Serial.
  70. *
  71. * @return true when the given key worked, false otherwise.
  72. */
  73. bool try_key(MFRC522::MIFARE_Key *key)
  74. {
  75. bool result = false;
  76. byte buffer[18];
  77. byte block = 0;
  78. MFRC522::StatusCode status;
  79. // Serial.println(F("Authenticating using key A..."));
  80. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
  81. if (status != MFRC522::STATUS_OK) {
  82. // Serial.print(F("PCD_Authenticate() failed: "));
  83. // Serial.println(mfrc522.GetStatusCodeName(status));
  84. return false;
  85. }
  86. // Read block
  87. byte byteCount = sizeof(buffer);
  88. status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
  89. if (status != MFRC522::STATUS_OK) {
  90. // Serial.print(F("MIFARE_Read() failed: "));
  91. // Serial.println(mfrc522.GetStatusCodeName(status));
  92. }
  93. else {
  94. // Successful read
  95. result = true;
  96. Serial.print(F("Success with key:"));
  97. dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
  98. Serial.println();
  99. // Dump block data
  100. Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
  101. dump_byte_array(buffer, 16);
  102. Serial.println();
  103. }
  104. Serial.println();
  105. mfrc522.PICC_HaltA(); // Halt PICC
  106. mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  107. return result;
  108. }
  109. /*
  110. * Main loop.
  111. */
  112. void loop() {
  113. // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  114. if ( ! mfrc522.PICC_IsNewCardPresent())
  115. return;
  116. // Select one of the cards
  117. if ( ! mfrc522.PICC_ReadCardSerial())
  118. return;
  119. // Show some details of the PICC (that is: the tag/card)
  120. Serial.print(F("Card UID:"));
  121. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  122. Serial.println();
  123. Serial.print(F("PICC type: "));
  124. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  125. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  126. // Try the known default keys
  127. MFRC522::MIFARE_Key key;
  128. for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  129. // Copy the known key into the MIFARE_Key structure
  130. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  131. key.keyByte[i] = knownKeys[k][i];
  132. }
  133. // Try the key
  134. if (try_key(&key)) {
  135. // Found and reported on the key and block,
  136. // no need to try other keys for this PICC
  137. break;
  138. }
  139. // http://arduino.stackexchange.com/a/14316
  140. if ( ! mfrc522.PICC_IsNewCardPresent())
  141. break;
  142. if ( ! mfrc522.PICC_ReadCardSerial())
  143. break;
  144. }
  145. }