schoenv

Untitled

Apr 20th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. /**************************************************************************/
  2. /*!
  3. @file mifareclassic_memdump.pde
  4. @author Adafruit Industries
  5. @license BSD (see license.txt)
  6.  
  7. This example attempts to dump the contents of a Mifare Classic 1K card
  8.  
  9. Note that you need the baud rate to be 115200 because we need to print
  10. out the data and read from the card at the same time!
  11.  
  12. This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
  13. This library works with the Adafruit NFC breakout
  14. ----> https://www.adafruit.com/products/364
  15.  
  16. Check out the links above for our tutorials and wiring diagrams
  17. These chips use SPI or I2C to communicate
  18.  
  19. Adafruit invests time and resources providing this open source code,
  20. please support Adafruit and open-source hardware by purchasing
  21. products from Adafruit!
  22.  
  23. */
  24. /**************************************************************************/
  25.  
  26. #include <Wire.h>
  27. #include <SPI.h>
  28. #include <Adafruit_PN532.h>
  29.  
  30. // If using the breakout with SPI, define the pins for SPI communication.
  31. #define PN532_SCK (2)
  32. #define PN532_MOSI (3)
  33. #define PN532_SS (4)
  34. #define PN532_MISO (5)
  35.  
  36. // If using the breakout or shield with I2C, define just the pins connected
  37. // to the IRQ and reset lines. Use the values below (2, 3) for the shield!
  38. #define PN532_IRQ (2)
  39. #define PN532_RESET (3) // Not connected by default on the NFC Shield
  40.  
  41. // Uncomment just _one_ line below depending on how your breakout or shield
  42. // is connected to the Arduino:
  43.  
  44. // Use this line for a breakout with a SPI connection:
  45. //Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
  46.  
  47. // Use this line for a breakout with a hardware SPI connection. Note that
  48. // the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
  49. // hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
  50. // SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
  51. //Adafruit_PN532 nfc(PN532_SS);
  52.  
  53. // Or use this line for a breakout or shield with an I2C connection:
  54. Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
  55.  
  56. #if defined(ARDUINO_ARCH_SAMD)
  57. // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  58. // also change #define in Adafruit_PN532.cpp library file
  59. #define Serial SerialUSB
  60. #endif
  61.  
  62. void setup(void) {
  63. #ifndef ESP8266
  64. while (!Serial); // for Leonardo/Micro/Zero
  65. #endif
  66. // has to be fast to dump the entire memory contents!
  67. Serial.begin(115200);
  68. Serial.println("Looking for PN532...");
  69.  
  70. nfc.begin();
  71.  
  72. uint32_t versiondata = nfc.getFirmwareVersion();
  73. if (! versiondata) {
  74. Serial.print("Didn't find PN53x board");
  75. while (1); // halt
  76. }
  77. // Got ok data, print it out!
  78. Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  79. Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  80. Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  81.  
  82. // configure board to read RFID tags
  83. nfc.SAMConfig();
  84.  
  85. Serial.println("Waiting for an ISO14443A Card ...");
  86. }
  87.  
  88.  
  89. void loop(void) {
  90. uint8_t success; // Flag to check if there was an error with the PN532
  91. uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
  92. uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  93. uint8_t currentblock; // Counter to keep track of which block we're on
  94. bool authenticated = true; // Flag to indicate if the sector is authenticated
  95. uint8_t data[16]; // Array to store block data during reads
  96.  
  97. // Keyb on NDEF and Mifare Classic should be the same
  98. uint8_t keyuniversal[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  99.  
  100. // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
  101. // 'uid' will be populated with the UID, and uidLength will indicate
  102. // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  103. success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  104.  
  105. if (success) {
  106. // Display some basic information about the card
  107. Serial.println("Found an ISO14443A card");
  108. Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
  109. Serial.print(" UID Value: ");
  110. nfc.PrintHex(uid, uidLength);
  111. Serial.println("");
  112.  
  113. if (uidLength == 4)
  114. {
  115. // We probably have a Mifare Classic card ...
  116. Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
  117.  
  118. // Now we try to go through all 16 sectors (each having 4 blocks)
  119. // authenticating each sector, and then dumping the blocks
  120. for (currentblock = 0; currentblock < 64; currentblock++)
  121. {
  122. // Check if this is a new block so that we can reauthenticate
  123. if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false;
  124.  
  125. // If the sector hasn't been authenticated, do so first
  126. if (!authenticated)
  127. {
  128. // Starting of a new sector ... try to to authenticate
  129. Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------");
  130. if (currentblock == 0)
  131. {
  132. // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
  133. // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a,
  134. // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
  135. success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
  136. }
  137. else
  138. {
  139. // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
  140. // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a,
  141. // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)
  142. success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
  143. }
  144. if (success)
  145. {
  146. authenticated = true;
  147. }
  148. else
  149. {
  150. Serial.println("Authentication error");
  151. }
  152. }
  153. // If we're still not authenticated just skip the block
  154. if (!authenticated)
  155. {
  156. Serial.print("Block ");Serial.print(currentblock, DEC);Serial.println(" unable to authenticate");
  157. }
  158. else
  159. {
  160. // Authenticated ... we should be able to read the block now
  161. // Dump the data into the 'data' array
  162. success = nfc.mifareclassic_ReadDataBlock(currentblock, data);
  163. if (success)
  164. {
  165. // Read successful
  166. Serial.print("Block ");Serial.print(currentblock, DEC);
  167. if (currentblock < 10)
  168. {
  169. Serial.print(" ");
  170. }
  171. else
  172. {
  173. Serial.print(" ");
  174. }
  175. // Dump the raw data
  176. nfc.PrintHexChar(data, 16);
  177. }
  178. else
  179. {
  180. // Oops ... something happened
  181. Serial.print("Block ");Serial.print(currentblock, DEC);
  182. Serial.println(" unable to read this block");
  183. }
  184. }
  185. }
  186. }
  187. else
  188. {
  189. Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
  190. }
  191. }
  192. // Wait a bit before trying again
  193. Serial.println("\n\nSend a character to run the mem dumper again!");
  194. Serial.flush();
  195. while (!Serial.available());
  196. while (Serial.available()) {
  197. Serial.read();
  198. }
  199. Serial.flush();
  200. }
Advertisement
Add Comment
Please, Sign In to add comment