Advertisement
earlvanze

RFID Password Typer

Aug 7th, 2017
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.14 KB | None | 0 0
  1.  
  2. /*
  3. // Based on code by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
  4.  // and code from HC Gilje - http://hcgilje.wordpress.com/resources/rfid_id12_tagreader/
  5.  // Modified for Arudino by djmatic
  6.  // Modified for ID-12 and checksum by Martijn The - http://www.martijnthe.nl/
  7.  //
  8.  // Use the drawings from HC Gilje to wire up the ID-12.
  9.  // Remark: disconnect the rx serial wire to the ID-12 when uploading the sketch
  10.  */
  11.  
  12. #include <EEPROM.h> // Needed to write to EEPROM storage
  13.  
  14. #define powerPin    10
  15. #define failPin     11
  16. #define passPin     12
  17. //#define doorPin     13
  18.  
  19. boolean programMode = false;
  20. boolean match = false;
  21.  
  22. byte storedCard[6];  // Stores an ID read from EEPROM
  23. byte readCard[6];    // Sotres an ID read from the RFID reader
  24. byte checksum = 0;   // Stores the checksum to verify the ID
  25.  
  26. void setup()
  27. {
  28.   //  for (int i = 0; i < 512; i++) // Uncoment to wipe the EEPROM
  29.   //   EEPROM.write(i, 0);
  30.   pinMode(powerPin, OUTPUT);      // Connected to Blue on tri-color LED to indicate reader is ready
  31.   //  pinMode(passPin, OUTPUT);       // Connected to Green on tri-color LED to indicate user is valid
  32.   //  pinMode(failPin, OUTPUT);       // Connected to Green on tri-color LED to indicate user is NOT valid or read failed
  33.   //  pinMode(doorPin, OUTPUT);       // Connected to relay to activate the door lock
  34.   Serial.begin(9600);             // Connect to the serial port
  35. }
  36.  
  37. void loop ()
  38. {
  39.  
  40.   // Website URLs:
  41.   String site_1 = "fb.com\n";
  42.   String site_2 = "home.nyu.edu\n";
  43.   String site_3 = "twitter.com\n";
  44.   String site_4 = "twitter.com\n";
  45.  
  46.   // Username:
  47.   String user_1 = "user@gmail.com\t";
  48.   String user_2 = "username\t";
  49.   String user_3 = "user3\t";
  50.   String user_4 = "user4\t";
  51.  
  52.   // Password:
  53.   String pass_1 = "password\n";
  54.   String pass_2 = "password\n";
  55.   String pass_3 = "password\n";
  56.   String pass_4 = "password\n";
  57.  
  58.   byte val = 0;       // Temp variable to hold the current byte
  59.  
  60.   normalModeOn();     // Normal mode, blue Power LED is on, all others are off
  61.  
  62.   if (programMode)   // Program mode to add a new ID card
  63.   {
  64.     programModeOn();  // Program Mode cycles through RGB waiting to read a new card
  65.  
  66.     if(Serial.available() > 0)  // Waits for something to come on the serial line
  67.     {
  68.       if((val = Serial.read()) == 2)  // First Byte should be 2, STX byte
  69.       {  
  70.         getID();                      // Get the ID, sets readCard = to the read ID
  71.         if ( !isMaster(readCard) )    // Check to see if it is the master programing card
  72.         {
  73.           writeID(readCard);          // If not, write the card to the EEPROM sotrage
  74.           programMode = false;        // Turn off programing mode
  75.           checksum = 0;               // Make sure the checksum is empty
  76.         }
  77.       }
  78.     }
  79.   }
  80.   // Normal Operation...
  81.   else
  82.   {
  83.     if(Serial.available() > 0)          // If the serial port is available and sending data...
  84.     {
  85.       if((val = Serial.read()) == 2)    // First Byte should be 2, STX byte
  86.       {                  
  87.         getID();                        // Get the ID, sets readCard = to the read ID
  88.         byte bytesread = 0;
  89.  
  90.         for ( int i = 0; i < 5; i++ )         // Loop 5 times
  91.         {
  92.           if  ( readCard[i] < 16 )              // Print out 0 if < 16 to prepend output
  93.             Serial.print("0");
  94.  
  95.           Serial.print(readCard[i], HEX);     // Print out the hex value read in
  96.           Serial.print(" ");
  97.         }
  98.         Serial.println();
  99.         Serial.print("Checksum: ");
  100.         Serial.print(readCard[5], HEX);       // Checksum read from the card
  101.  
  102.         if ( readCard[5] == checksum )        // See if the 5th BYTE (the checksum) read in from the reader
  103.         {                                     // matches the checksum caculated
  104.           checksum = 0;                       // If so, we can empty the variable storing the calculated checksum
  105.           Serial.println(" passed");  
  106.           Serial.println();
  107.           if ( isMaster( readCard ) )         // Check to see if the card is the master programing card
  108.           {
  109.             programMode = true;               // If so, enable programing mode
  110.           }
  111.           else
  112.           {
  113.             if ( findID(readCard) )           // If not, see if the card is in the EEPROM
  114.             {
  115.               openSite();                    // If it is, open the door lock
  116.             }
  117.             else
  118.             {
  119.               failed();                       // If not, show that the ID was not valid
  120.             }
  121.           }
  122.         }
  123.         else                                  // If the checksum failed
  124.         {                                     // Print out the checksum
  125.           /*
  126.           Serial.println(" error");
  127.            Serial.println();
  128.            Serial.print("[");
  129.            Serial.print(readCard[5], HEX);
  130.            Serial.print("] != [");
  131.            Serial.print(checksum, HEX);
  132.            Serial.print("] ");
  133.            */
  134.         }
  135.       }
  136.     }
  137.   }
  138. }
  139.  
  140. // If the serial port is ready and we received the STX BYTE (2) then this function is called
  141. // to get the 4 BYTE ID + 1 BYTE checksum. The ID+checksum is stored in readCard[6]
  142. // Bytes 0-4 are the 5 ID bytes, byte 5 is the checksum
  143. void getID()
  144. {
  145.   byte bytesread = 0;
  146.   byte i = 0;
  147.   byte val = 0;
  148.   byte tempbyte = 0;
  149.  
  150.   // 5 HEX Byte code is actually 10 ASCII Bytes.
  151.   while ( bytesread < 12 ) // Read 10 digit code + 2 digit checksum
  152.   {                        
  153.     if( Serial.available() > 0)   // Check to make sure data is coming on the serial line
  154.     {
  155.       val = Serial.read();        // Store the current ASCII byte in val
  156.  
  157.       if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02))
  158.       {                           // If header or stop bytes before the 10 digit reading
  159.         break;                    // Stop reading                                
  160.       }
  161.  
  162.       if ( (val >= '0' ) && ( val <= '9' ) )    // Do Ascii/Hex conversion
  163.       {
  164.         val = val - '0';
  165.       }
  166.       else if ( ( val >= 'A' ) && ( val <= 'F' ) )
  167.       {
  168.         val = 10 + val - 'A';
  169.       }
  170.  
  171.       if ( bytesread & 1 == 1 )      // Every two ASCII charactors = 1 BYTE in HEX format
  172.       {
  173.         // Make some space for this hex-digit by
  174.         // shifting the previous hex-digit with 4 bits to the left:
  175.         readCard[bytesread >> 1] = (val | (tempbyte << 4));
  176.  
  177.         if ( bytesread >> 1 != 5 )                // If we're at the checksum byte,
  178.         {
  179.           checksum ^= readCard[bytesread >> 1];   // Calculate the checksum using XOR
  180.         };
  181.       }
  182.       else                                        // If it is the first HEX charactor
  183.       {
  184.         tempbyte = val;                           // Store the HEX in a temp variable
  185.       };
  186.       bytesread++;                                // Increment the counter to keep track
  187.     }
  188.   }
  189.   bytesread = 0;
  190. }
  191.  
  192.  
  193. // Read an ID from EEPROM and save it to the storedCard[6] array
  194. void readID( int number )  // Number = position in EEPROM to get the 5 Bytes from
  195. {
  196.   int start = (number * 5 ) - 4;  // Figure out starting position
  197.   //Serial.print("Start: ");
  198.   //Serial.print(start);
  199.   //Serial.print("\n\n");
  200.  
  201.   for ( int i = 0; i < 5; i++ )  // Loop 5 times to get the 5 Bytes
  202.   {
  203.     storedCard[i] = EEPROM.read(start+i);  // Assign values read from EEPROM to array
  204.     /*
  205.      Serial.print("Read [");
  206.      Serial.print(start+i);
  207.      Serial.print("] [");
  208.      Serial.print(storedCard[i], HEX);
  209.      Serial.print("] \n");
  210.      */
  211.   }
  212. }
  213.  
  214. // Write an array to the EEPROM in the next available slot
  215. void writeID( byte a[] )
  216. {
  217.   if ( !findID( a ) )          // Before we write to the EEPROM, check to see if we have seen this card before!
  218.   {
  219.     int num = EEPROM.read(0);  // Get the numer of used spaces, position 0 stores the number of ID cards
  220.     /*
  221.     Serial.print("Num: ");
  222.      Serial.print(num);
  223.      Serial.print(" \n");
  224.      */
  225.     int start = ( num * 5 ) + 1;   // Figure out where the next slot starts
  226.  
  227.     num++;                         // Increment the counter by one
  228.     EEPROM.write( 0, num );        // Write the new count to the counter
  229.  
  230.     for ( int j = 0; j < 5; j++ )  // Loop 5 times
  231.     {
  232.       EEPROM.write( start+j, a[j] );  // Write the array values to EEPROM in the right position
  233.       /*
  234.       Serial.print("W[");
  235.        Serial.print(start+j);
  236.        Serial.print("] Value [");
  237.        Serial.print(a[j], HEX);
  238.        Serial.print("] \n");
  239.        */
  240.     }
  241.     successWrite();
  242.   }
  243.   else
  244.   {
  245.     failedWrite();
  246.   }
  247. }
  248.  
  249. // Check two arrays of bytes to see if they are exact matches
  250. boolean checkTwo ( byte a[], byte b[] )
  251. {
  252.   if ( a[0] != NULL )             // Make sure there is something in the array first
  253.     match = true;                 // Assume they match at first
  254.  
  255.   for ( int k = 0;  k < 5; k++ )  // Loop 5 times
  256.   {
  257.     /*
  258.     Serial.print("[");
  259.      Serial.print(k);
  260.      Serial.print("] ReadCard [");
  261.      Serial.print(a[k], HEX);
  262.      Serial.print("] StoredCard [");
  263.      Serial.print(b[k], HEX);
  264.      Serial.print("] \n");
  265.      */
  266.     if ( a[k] != b[k] )           // IF a != b then set match = false, one fails, all fail
  267.       match = false;
  268.   }
  269.   if ( match )                    // Check to see if if match is still true
  270.   {
  271.     //Serial.print("Strings Match! \n");  
  272.     return true;                  // Return true
  273.   }
  274.   else {
  275.     //Serial.print("Strings do not match \n");
  276.     return false;                 // Return false
  277.   }
  278. }
  279.  
  280. // Looks in the EEPROM to try to match any of the EEPROM ID's with the passed ID
  281. boolean findID( byte find[] )
  282. {
  283.   int count = EEPROM.read(0);             // Read the first Byte of EEPROM that
  284.   // Serial.print("Count: ");                // stores the number of ID's in EEPROM
  285.   // Serial.print(count);
  286.   //Serial.print("\n");
  287.   for ( int i = 1; i <= count; i++ )      // Loop once for each EEPROM entry
  288.   {
  289.     readID(i);                            // Read an ID from EEPROM, it is stored in storedCard[6]
  290.     if( checkTwo( find, storedCard ) )    // Check to see if the storedCard read from EEPROM
  291.     {                                     // is the same as the find[] ID card passed
  292.       //Serial.print("We have a matched card!!! \n");
  293.       return true;
  294.       break;                              // Stop looking we found it
  295.     }
  296.     else                                  // If not, return false
  297.     {
  298.       //Serial.print("No Match here.... \n");
  299.     }
  300.  
  301.   }
  302.   return false;
  303. }
  304.  
  305. // Opens door and turns on the green LED for setDelay seconds
  306. void openSite()
  307. {
  308.  
  309.   String site_1 = "fb.com\n";
  310.   String user_1 = "user@gmail.com\t";
  311.   String pass_1 = "password\n";
  312.   //   Keyboard.print("It works!");
  313.   digitalWrite(powerPin, LOW);  // Turn off blue LED
  314.   delay(500);
  315.   digitalWrite(powerPin, HIGH); // Turn on blue LED
  316.   // Open new tab and navigate to URL
  317.   Keyboard.press(KEY_LEFT_GUI);
  318.   Keyboard.write('t');
  319.   Keyboard.releaseAll();
  320.   delay(1000);
  321.   Keyboard.print(site_1);
  322.   delay(2000);
  323.  
  324.   // enter username and password
  325.   Keyboard.print(user_1);
  326.   Keyboard.print(pass_1);
  327. }
  328.  
  329. // Flashes Red LED if failed login
  330. void failed()
  331. {
  332.   //  digitalWrite(passPin, LOW);   // Make sure green LED is off
  333.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  334.   delay(200);
  335.   digitalWrite(powerPin, HIGH);  // Make sure blue LED is off
  336.   delay(200);
  337.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  338.   delay(200);
  339.   digitalWrite(powerPin, HIGH);  // Make sure blue LED is off
  340.   delay(200);
  341.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  342.   delay(200);
  343.   digitalWrite(powerPin, HIGH);  // Make sure blue LED is off
  344.   delay(200);
  345. }
  346.  
  347. // Check to see if the ID passed is the master programing card
  348. boolean isMaster( byte test[] )
  349. {
  350.   byte bytesread = 0;
  351.   byte i = 0;                // Example card, replace with one of yours you want to be the master
  352.   byte val[10] = {'0','0','0','2','1','1','7','7','7','7'};  
  353.   byte master[6];
  354.   byte checksum = 0;
  355.   byte tempbyte = 0;
  356.   bytesread = 0;
  357.  
  358.   for ( i = 0; i < 10; i++ )  // First we need to convert the array above into a 5 HEX BYTE array
  359.   {
  360.     if ( (val[i] >= '0' ) && ( val[i] <= '9' ) )   // Convert one char to HEX
  361.     {
  362.       val[i] = val[i] - '0';
  363.     }
  364.     else if ( (val[i] >= 'A' ) && ( val[i] <= 'F' ) )
  365.     {
  366.       val[i] = 10 + val[i] - 'A';
  367.     }
  368.  
  369.     if (bytesread & 1 == 1) // Every two hex-digits, add byte to code:
  370.     {
  371.       // make some space for this hex-digit by
  372.       // shifting the previous hex-digit with 4 bits to the left:
  373.       master[bytesread >> 1] = (val[i] | (tempbyte << 4));
  374.  
  375.       if (bytesread >> 1 != 5)                 // If we're at the checksum byte,
  376.       {
  377.         checksum ^= master[bytesread >> 1];      // Calculate the checksum... (XOR)
  378.       };
  379.     }
  380.     else
  381.     {
  382.       tempbyte = val[i];                       // Store the first hex digit first...
  383.     };
  384.     bytesread++;        
  385.   }
  386.  
  387.   if ( checkTwo( test, master ) )                // Check to see if the master = the test ID
  388.     return true;
  389.   else
  390.     return false;
  391. }
  392.  
  393. // Controls LED's for Normal mode, Blue on, all others off
  394. void normalModeOn()
  395. {
  396.   digitalWrite(powerPin, HIGH);    // Power pin ON and ready to read card
  397. }
  398.  
  399. // Controls LED's for program mode, cycles through RGB
  400. void programModeOn()
  401. {
  402.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  403.   //  digitalWrite(failPin, LOW);   // Make sure red LED is off
  404.   //  digitalWrite(passPin, HIGH);  // Make sure green LED is on
  405.   delay(200);
  406.   digitalWrite(powerPin, HIGH);  // Make sure blue LED is off
  407.   //  digitalWrite(failPin, HIGH);  // Make sure red LED is on
  408.   //  digitalWrite(passPin, LOW);   // Make sure green LED is off
  409.   delay(200);
  410.   digitalWrite(powerPin, LOW); // Make sure blue LED is on
  411.   //  digitalWrite(failPin, LOW);   // Make sure red LED is off
  412.   //  digitalWrite(passPin, LOW);   // Make sure green LED is off
  413.   delay(200);
  414. }
  415.  
  416. // Flashes the green LED 3 times to indicate a successful write to EEPROM
  417. void successWrite()
  418. {
  419.   digitalWrite(powerPin, HIGH); // Make sure blue LED is off
  420.   //  digitalWrite(failPin, LOW);  // Make sure blue LED is off
  421.   //  digitalWrite(passPin, HIGH); // Make sure green LED is on
  422.   delay(200);
  423.   digitalWrite(powerPin, LOW);  // Make sure green LED is off
  424.   delay(200);
  425.   digitalWrite(powerPin, HIGH); // Make sure green LED is on
  426.   delay(200);
  427.   digitalWrite(powerPin, LOW);  // Make sure green LED is off
  428.   delay(200);
  429.   digitalWrite(powerPin, HIGH); // Make sure green LED is on
  430.   delay(200);
  431. }
  432.  
  433. // Flashes the red LED 3 times to indicate a failed write to EEPROM
  434. void failedWrite()
  435. {
  436.   digitalWrite(powerPin, HIGH); // Make sure blue LED is off
  437.   //  digitalWrite(failPin, HIGH); // Make sure red LED is on
  438.   //  digitalWrite(passPin, LOW);  // Make sure green LED is off
  439.   delay(200);
  440.   digitalWrite(powerPin, HIGH);  // Make sure red LED is off
  441.   delay(200);
  442.   //  digitalWrite(failPin, HIGH); // Make sure red LED is on
  443.   //  delay(200);
  444.   //  digitalWrite(failPin, LOW);  // Make sure red LED is off
  445.   //  delay(200);
  446.   //  digitalWrite(failPin, HIGH); // Make sure red LED is on
  447.   //  delay(200);
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement