PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

313 lines
10KB

  1. /*
  2. * Copy the RFID card data into variables and then
  3. * scan the second empty card to copy all the date
  4. * ----------------------------------------------------------------------------
  5. * Example sketch/program which will try the most used default keys listed in
  6. * https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys to dump the
  7. * block 0 of a MIFARE RFID card using a RFID-RC522 reader.
  8. *
  9. * Typical pin layout used:
  10. * -----------------------------------------------------------------------------------------
  11. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  12. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  13. * Signal Pin Pin Pin Pin Pin Pin
  14. * -----------------------------------------------------------------------------------------
  15. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  16. * SPI SS SDA(SS) 10 53 D10 10 10
  17. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  18. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  19. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  20. *
  21. */
  22. #include <SPI.h>
  23. #include <MFRC522.h>
  24. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  25. constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
  26. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  27. byte buffer[18];
  28. byte block;
  29. byte waarde[64][16];
  30. MFRC522::StatusCode status;
  31. MFRC522::MIFARE_Key key;
  32. // Number of known default keys (hard-coded)
  33. // NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
  34. constexpr uint8_t NR_KNOWN_KEYS = 8;
  35. // Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
  36. byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
  37. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
  38. {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
  39. {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
  40. {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
  41. {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
  42. {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
  43. {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
  44. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
  45. };
  46. char choice;
  47. /*
  48. * Initialize.
  49. */
  50. void setup() {
  51. Serial.begin(9600); // Initialize serial communications with the PC
  52. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  53. SPI.begin(); // Init SPI bus
  54. mfrc522.PCD_Init(); // Init MFRC522 card
  55. Serial.println(F("Try the most used default keys to print block 0 to 63 of a MIFARE PICC."));
  56. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  57. for (byte i = 0; i < 6; i++) {
  58. key.keyByte[i] = 0xFF;
  59. }
  60. }
  61. //Via seriele monitor de bytes uitlezen in hexadecimaal
  62. void dump_byte_array(byte *buffer, byte bufferSize) {
  63. for (byte i = 0; i < bufferSize; i++) {
  64. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  65. Serial.print(buffer[i], HEX);
  66. }
  67. }
  68. //Via seriele monitor de bytes uitlezen in ASCI
  69. void dump_byte_array1(byte *buffer, byte bufferSize) {
  70. for (byte i = 0; i < bufferSize; i++) {
  71. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  72. Serial.write(buffer[i]);
  73. }
  74. }
  75. /*
  76. * Try using the PICC (the tag/card) with the given key to access block 0 to 63.
  77. * On success, it will show the key details, and dump the block data on Serial.
  78. *
  79. * @return true when the given key worked, false otherwise.
  80. */
  81. boolean try_key(MFRC522::MIFARE_Key *key)
  82. {
  83. boolean result = false;
  84. for(byte block = 0; block < 64; block++){
  85. // Serial.println(F("Authenticating using key A..."));
  86. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
  87. if (status != MFRC522::STATUS_OK) {
  88. Serial.print(F("PCD_Authenticate() failed: "));
  89. Serial.println(mfrc522.GetStatusCodeName(status));
  90. return false;
  91. }
  92. // Read block
  93. byte byteCount = sizeof(buffer);
  94. status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
  95. if (status != MFRC522::STATUS_OK) {
  96. Serial.print(F("MIFARE_Read() failed: "));
  97. Serial.println(mfrc522.GetStatusCodeName(status));
  98. }
  99. else {
  100. // Successful read
  101. result = true;
  102. Serial.print(F("Success with key:"));
  103. dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
  104. Serial.println();
  105. // Dump block data
  106. Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
  107. dump_byte_array1(buffer, 16); //omzetten van hex naar ASCI
  108. Serial.println();
  109. for (int p = 0; p < 16; p++) //De 16 bits uit de block uitlezen
  110. {
  111. waarde [block][p] = buffer[p];
  112. Serial.print(waarde[block][p]);
  113. Serial.print(" ");
  114. }
  115. }
  116. }
  117. Serial.println();
  118. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  119. mfrc522.PICC_HaltA(); // Halt PICC
  120. mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  121. return result;
  122. start();
  123. }
  124. /*
  125. * Main loop.
  126. */
  127. void loop() {
  128. start();
  129. }
  130. void start(){
  131. choice = Serial.read();
  132. if(choice == '1')
  133. {
  134. Serial.println("Read the card");
  135. keuze1();
  136. }
  137. else if(choice == '2')
  138. {
  139. Serial.println("See what is in the variables");
  140. keuze2();
  141. }
  142. else if(choice == '3')
  143. {
  144. Serial.println("Copying the data on to the new card");
  145. keuze3();
  146. }
  147. }
  148. void keuze2(){ //Test waardes in blokken
  149. for(block = 4; block <= 62; block++){
  150. if(block == 7 || block == 11 || block == 15 || block == 19 || block == 23 || block == 27 || block == 31 || block == 35 || block == 39 || block == 43 || block == 47 || block == 51 || block == 55 || block == 59){
  151. block ++;
  152. }
  153. Serial.print(F("Writing data into block "));
  154. Serial.print(block);
  155. Serial.println("\n");
  156. for(int j = 0; j < 16; j++){
  157. Serial.print(waarde[block][j]);
  158. Serial.print(" ");
  159. }
  160. Serial.println("\n");
  161. }
  162. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  163. start();
  164. }
  165. void keuze3(){ //Copy the data in the new card
  166. Serial.println("Insert new card...");
  167. // Look for new cards
  168. if ( ! mfrc522.PICC_IsNewCardPresent())
  169. return;
  170. // Select one of the cards
  171. if ( ! mfrc522.PICC_ReadCardSerial())
  172. return;
  173. // Show some details of the PICC (that is: the tag/card)
  174. Serial.print(F("Card UID:"));
  175. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  176. Serial.println();
  177. Serial.print(F("PICC type: "));
  178. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  179. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  180. // Try the known default keys
  181. /*MFRC522::MIFARE_Key key;
  182. for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  183. // Copy the known key into the MIFARE_Key structure
  184. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  185. key.keyByte[i] = knownKeys[k][i];
  186. }
  187. }*/
  188. for (byte i = 0; i < 6; i++) {
  189. key.keyByte[i] = 0xFF;
  190. }
  191. for(int i = 4; i <= 62; i++){ //De blocken 4 tot 62 kopieren, behalve al deze onderstaande blocken (omdat deze de authenticatie blokken zijn)
  192. if(i == 7 || i == 11 || i == 15 || i == 19 || i == 23 || i == 27 || i == 31 || i == 35 || i == 39 || i == 43 || i == 47 || i == 51 || i == 55 || i == 59){
  193. i++;
  194. }
  195. block = i;
  196. // Authenticate using key A
  197. Serial.println(F("Authenticating using key A..."));
  198. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  199. if (status != MFRC522::STATUS_OK) {
  200. Serial.print(F("PCD_Authenticate() failed: "));
  201. Serial.println(mfrc522.GetStatusCodeName(status));
  202. return;
  203. }
  204. // Authenticate using key B
  205. Serial.println(F("Authenticating again using key B..."));
  206. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, block, &key, &(mfrc522.uid));
  207. if (status != MFRC522::STATUS_OK) {
  208. Serial.print(F("PCD_Authenticate() failed: "));
  209. Serial.println(mfrc522.GetStatusCodeName(status));
  210. return;
  211. }
  212. // Write data to the block
  213. Serial.print(F("Writing data into block "));
  214. Serial.print(block);
  215. Serial.println("\n");
  216. dump_byte_array(waarde[block], 16);
  217. status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(block, waarde[block], 16);
  218. if (status != MFRC522::STATUS_OK) {
  219. Serial.print(F("MIFARE_Write() failed: "));
  220. Serial.println(mfrc522.GetStatusCodeName(status));
  221. }
  222. Serial.println("\n");
  223. }
  224. mfrc522.PICC_HaltA(); // Halt PICC
  225. mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  226. Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
  227. start();
  228. }
  229. void keuze1(){ //Read card
  230. Serial.println("Insert card...");
  231. // Look for new cards
  232. if ( ! mfrc522.PICC_IsNewCardPresent())
  233. return;
  234. // Select one of the cards
  235. if ( ! mfrc522.PICC_ReadCardSerial())
  236. return;
  237. // Show some details of the PICC (that is: the tag/card)
  238. Serial.print(F("Card UID:"));
  239. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  240. Serial.println();
  241. Serial.print(F("PICC type: "));
  242. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  243. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  244. // Try the known default keys
  245. MFRC522::MIFARE_Key key;
  246. for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
  247. // Copy the known key into the MIFARE_Key structure
  248. for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
  249. key.keyByte[i] = knownKeys[k][i];
  250. }
  251. // Try the key
  252. if (try_key(&key)) {
  253. // Found and reported on the key and block,
  254. // no need to try other keys for this PICC
  255. break;
  256. }
  257. }
  258. }