Advertisement
jamesdylangoldstein

CS50MD5BruteForceFourDigit

Jun 11th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.88 KB | None | 0 0
  1. //  For CS50 Hacker PSet2
  2. // 1) Don't store your passwords as MD5, it's too easy to break
  3. // 2) Don't use this to break someone's password, it's an exercise for a class
  4. // 3) This code will take forever to run, maybe weeks, maybe years!
  5. // 4) Sample password is easy to crack, but only a supercomputer would be able to run it on strong password
  6. //  Created by James Dylan Goldstein on 6/8/16.
  7. //  Copyright © 2016 James Dylan Goldstein. All rights reserved.
  8. //
  9.  
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include "cs50.h"
  14. #define _XOPEN_SOURCE
  15. #define _GNU_SOURCE
  16.  
  17. char *crypt(const char *key, const char *salt);
  18. //char *crypt_r(const char *key, const char *salt, struct crypt_data *data);
  19.  
  20. int main(int argc, const char * argv[])
  21. {
  22.     string searchsalt = "50";
  23.     string crackedpassword;
  24.     int check;
  25.     string username = "caesar";
  26.     string givenhash = "50zPJlUFIYY0o";
  27.     char bruteforce[20];
  28.    
  29.     // Brute force check one digit strings
  30.     for (int arrayvalueone1digit = 0; arrayvalueone1digit < 1; arrayvalueone1digit++)
  31.     {
  32.         for (int asciicounterone1digit = 48; asciicounterone1digit <= 122; asciicounterone1digit++)
  33.         {
  34.             bruteforce[arrayvalueone1digit] = (char)asciicounterone1digit;
  35.                    
  36.             crackedpassword = crypt(bruteforce, searchsalt);
  37.             check = strcmp(crackedpassword, givenhash);
  38.             if (check == 0)
  39.             {
  40.                 printf("Username: %s\n Encrypted password: %s\n Password: %s \n", username, givenhash, bruteforce);
  41.                 return 0;
  42.             }
  43.         }
  44.     }
  45.    
  46.     // Brute force check two digit strings
  47.     for (int arrayvalueone2digit = 0; arrayvalueone2digit < 1; arrayvalueone2digit++)
  48.     {
  49.         for (int asciicounterone2digit = 48; asciicounterone2digit <= 122; asciicounterone2digit++)
  50.         {
  51.             bruteforce[arrayvalueone2digit] = (char)asciicounterone2digit;
  52.            
  53.             for (int arrayvaluetwo2digit = 1; arrayvaluetwo2digit < 2; arrayvaluetwo2digit++)
  54.             {
  55.                 for (int asciicountertwo2digit = 48; asciicountertwo2digit <= 122; asciicountertwo2digit++)
  56.                 {
  57.                     bruteforce[arrayvaluetwo2digit] = (char)asciicountertwo2digit;
  58.                            
  59.                     crackedpassword = crypt(bruteforce, searchsalt);
  60.                     check = strcmp(crackedpassword, givenhash);
  61.                     if (check == 0)
  62.                     {
  63.                         printf("Username: %s \nEncrypted password: %s\n Password: %s \n", username, givenhash, bruteforce);
  64.                         return 0;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.     }
  70.  
  71.    
  72.     // Brute force check three digit strings
  73.     for (int arrayvalueone3digit = 0; arrayvalueone3digit < 1; arrayvalueone3digit++)
  74.     {
  75.         for (int asciicounterone3digit = 48; asciicounterone3digit <= 122; asciicounterone3digit++)
  76.         {
  77.             bruteforce[arrayvalueone3digit] = (char)asciicounterone3digit;
  78.            
  79.             for (int arrayvaluetwo3digit = 1; arrayvaluetwo3digit < 2; arrayvaluetwo3digit++)
  80.             {
  81.                 for (int asciicountertwo3digit = 48; asciicountertwo3digit <= 122; asciicountertwo3digit++)
  82.                 {
  83.                     bruteforce[arrayvaluetwo3digit] = (char)asciicountertwo3digit;
  84.                    
  85.                     for (int arrayvaluethree3digit = 2; arrayvaluethree3digit < 3; arrayvaluethree3digit++)
  86.                     {
  87.                         for (int asciicounterthree3digit = 48; asciicounterthree3digit <= 122; asciicounterthree3digit++)
  88.                         {
  89.                             bruteforce[arrayvaluethree3digit] = (char)asciicounterthree3digit;
  90.                            
  91.                             crackedpassword = crypt(bruteforce, searchsalt);
  92.                             check = strcmp(crackedpassword, givenhash);
  93.                             if (check == 0)
  94.                             {
  95.                                 printf("Username: %s\nEncrypted password: %s\nPassword: %s \n", username, givenhash, bruteforce);
  96.                                 return 0;
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.    
  105.     // Brute force check four digit strings
  106.     for (int arrayvalueone4digit = 0; arrayvalueone4digit < 1; arrayvalueone4digit++)
  107.     {
  108.         for (int asciicounterone4digit = 48; asciicounterone4digit <= 122; asciicounterone4digit++)
  109.         {
  110.             bruteforce[arrayvalueone4digit] = (char)asciicounterone4digit;
  111.            
  112.             for (int arrayvaluetwo4digit = 1; arrayvaluetwo4digit < 2; arrayvaluetwo4digit++)
  113.             {
  114.                 for (int asciicountertwo4digit = 48; asciicountertwo4digit <= 122; asciicountertwo4digit++)
  115.                 {
  116.                     bruteforce[arrayvaluetwo4digit] = (char)asciicountertwo4digit;
  117.                    
  118.                     for (int arrayvaluethree4digit = 2; arrayvaluethree4digit < 3; arrayvaluethree4digit++)
  119.                     {
  120.                         for (int asciicounterthree4digit = 48; asciicounterthree4digit <= 122; asciicounterthree4digit++)
  121.                         {
  122.                             bruteforce[arrayvaluethree4digit] = (char)asciicounterthree4digit;
  123.                            
  124.                             for (int arrayvaluefour4digit = 3; arrayvaluefour4digit < 4; arrayvaluefour4digit++)
  125.                             {
  126.                                 for (int asciicounterfour4digit = 48; asciicounterfour4digit <= 122; asciicounterfour4digit++)
  127.                                 {
  128.                                     bruteforce[arrayvaluefour4digit] = (char)asciicounterfour4digit;
  129.                                    
  130.                                     crackedpassword = crypt(bruteforce, searchsalt);
  131.                                     check = strcmp(crackedpassword, givenhash);
  132.                                     if (check == 0)
  133.                                     {
  134.                                         printf("Username: %s \nEncrypted password: %s \nPassword: %s \n", username, givenhash, bruteforce);
  135.                                         return 0;
  136.                                     }
  137.                                 }
  138.                             }
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.         }
  144.     }
  145.    
  146.     // Brute force check five digit strings
  147.     for (int arrayvalueone5digit = 0; arrayvalueone5digit < 1; arrayvalueone5digit++)
  148.     {
  149.         for (int asciicounterone5digit = 48; asciicounterone5digit <= 122; asciicounterone5digit++)
  150.         {
  151.             bruteforce[arrayvalueone5digit] = (char)asciicounterone5digit;
  152.            
  153.             for (int arrayvaluetwo5digit = 1; arrayvaluetwo5digit < 2; arrayvaluetwo5digit++)
  154.             {
  155.                 for (int asciicountertwo5digit = 48; asciicountertwo5digit <= 122; asciicountertwo5digit++)
  156.                 {
  157.                     bruteforce[arrayvaluetwo5digit] = (char)asciicountertwo5digit;
  158.                    
  159.                     for (int arrayvaluethree5digit = 2; arrayvaluethree5digit < 3; arrayvaluethree5digit++)
  160.                     {
  161.                         for (int asciicounterthree5digit = 48; asciicounterthree5digit <= 122; asciicounterthree5digit++)
  162.                         {
  163.                             bruteforce[arrayvaluethree5digit] = (char)asciicounterthree5digit;
  164.                            
  165.                             for (int arrayvaluefour5digit = 3; arrayvaluefour5digit < 4; arrayvaluefour5digit++)
  166.                             {
  167.                                 for (int asciicounterfour5digit = 48; asciicounterfour5digit <= 122; asciicounterfour5digit++)
  168.                                 {
  169.                                     bruteforce[arrayvaluefour5digit] = (char)asciicounterfour5digit;
  170.                                    
  171.                                     for (int arrayvaluefive5digit = 4; arrayvaluefive5digit < 5; arrayvaluefive5digit++)
  172.                                     {
  173.                                         for (int asciicounterfive5digit = 48; asciicounterfive5digit <= 122; asciicounterfive5digit++)
  174.                                         {
  175.                                             bruteforce[arrayvaluefive5digit] = (char)asciicounterfive5digit;
  176.                                            
  177.                                             crackedpassword = crypt(bruteforce, searchsalt);
  178.                                             check = strcmp(crackedpassword, givenhash);
  179.                                             if (check == 0)
  180.                                             {
  181.                                                 printf("Username: %s \nEncrypted password: %s \nPassword: %s \n", username, givenhash, bruteforce);
  182.                                                 return 0;
  183.                                             }
  184.                                         }
  185.                                     }
  186.                                 }
  187.                             }
  188.                         }
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.     }
  194.    
  195.     // Brute force check six digit strings
  196.     for (int arrayvalueone6digit = 0; arrayvalueone6digit < 1; arrayvalueone6digit++)
  197.     {
  198.         for (int asciicounterone6digit = 48; asciicounterone6digit <= 122; asciicounterone6digit++)
  199.         {
  200.             bruteforce[arrayvalueone6digit] = (char)asciicounterone6digit;
  201.            
  202.             for (int arrayvaluetwo6digit = 1; arrayvaluetwo6digit < 2; arrayvaluetwo6digit++)
  203.             {
  204.                 for (int asciicountertwo6digit = 48; asciicountertwo6digit <= 122; asciicountertwo6digit++)
  205.                 {
  206.                     bruteforce[arrayvaluetwo6digit] = (char)asciicountertwo6digit;
  207.                    
  208.                     for (int arrayvaluethree6digit = 2; arrayvaluethree6digit < 3; arrayvaluethree6digit++)
  209.                     {
  210.                         for (int asciicounterthree6digit = 48; asciicounterthree6digit <= 122; asciicounterthree6digit++)
  211.                         {
  212.                             bruteforce[arrayvaluethree6digit] = (char)asciicounterthree6digit;
  213.                            
  214.                             for (int arrayvaluefour6digit = 3; arrayvaluefour6digit < 4; arrayvaluefour6digit++)
  215.                             {
  216.                                 for (int asciicounterfour6digit = 48; asciicounterfour6digit <= 122; asciicounterfour6digit++)
  217.                                 {
  218.                                     bruteforce[arrayvaluefour6digit] = (char)asciicounterfour6digit;
  219.                                    
  220.                                     for (int arrayvaluefive6digit = 4; arrayvaluefive6digit < 5; arrayvaluefive6digit++)
  221.                                     {
  222.                                         for (int asciicounterfive6digit = 48; asciicounterfive6digit <= 122; asciicounterfive6digit++)
  223.                                         {
  224.                                             bruteforce[arrayvaluefive6digit] = (char)asciicounterfive6digit;
  225.                                            
  226.                                             for (int arrayvaluesix6digit = 5; arrayvaluesix6digit < 6; arrayvaluesix6digit++)
  227.                                             {
  228.                                                 for (int asciicountersix6digit = 48; asciicountersix6digit <= 122; asciicountersix6digit++)
  229.                                                 {
  230.                                                     bruteforce[arrayvaluesix6digit] = (char)asciicountersix6digit;
  231.                                            
  232.                                                     crackedpassword = crypt(bruteforce, searchsalt);
  233.                                                     check = strcmp(crackedpassword, givenhash);
  234.                                                     if (check == 0)
  235.                                                     {
  236.                                                         printf("Username: %s \nEncrypted password: %s \nPassword: %s \n", username, givenhash, bruteforce);
  237.                                                         return 0;
  238.                                                     }
  239.                                                 }
  240.                                             }
  241.                                         }
  242.                                     }
  243.                                 }
  244.                             }
  245.                         }
  246.                     }
  247.                 }
  248.             }
  249.         }
  250.     }
  251.  
  252.     // Brute force check seven digit strings
  253.     for (int arrayvalueone7digit = 0; arrayvalueone7digit < 1; arrayvalueone7digit++)
  254.     {
  255.         for (int asciicounterone7digit = 48; asciicounterone7digit <= 122; asciicounterone7digit++)
  256.         {
  257.             bruteforce[arrayvalueone7digit] = (char)asciicounterone7digit;
  258.            
  259.             for (int arrayvaluetwo7digit = 1; arrayvaluetwo7digit < 2; arrayvaluetwo7digit++)
  260.             {
  261.                 for (int asciicountertwo7digit = 48; asciicountertwo7digit <= 122; asciicountertwo7digit++)
  262.                 {
  263.                     bruteforce[arrayvaluetwo7digit] = (char)asciicountertwo7digit;
  264.                    
  265.                     for (int arrayvaluethree7digit = 2; arrayvaluethree7digit < 3; arrayvaluethree7digit++)
  266.                     {
  267.                         for (int asciicounterthree7digit = 48; asciicounterthree7digit <= 122; asciicounterthree7digit++)
  268.                         {
  269.                             bruteforce[arrayvaluethree7digit] = (char)asciicounterthree7digit;
  270.                            
  271.                             for (int arrayvaluefour7digit = 3; arrayvaluefour7digit < 4; arrayvaluefour7digit++)
  272.                             {
  273.                                 for (int asciicounterfour7digit = 48; asciicounterfour7digit <= 122; asciicounterfour7digit++)
  274.                                 {
  275.                                     bruteforce[arrayvaluefour7digit] = (char)asciicounterfour7digit;
  276.                                    
  277.                                     for (int arrayvaluefive7digit = 4; arrayvaluefive7digit < 5; arrayvaluefive7digit++)
  278.                                     {
  279.                                         for (int asciicounterfive7digit = 48; asciicounterfive7digit <= 122; asciicounterfive7digit++)
  280.                                         {
  281.                                             bruteforce[arrayvaluefive7digit] = (char)asciicounterfive7digit;
  282.                                            
  283.                                             for (int arrayvaluesix7digit = 5; arrayvaluesix7digit < 6; arrayvaluesix7digit++)
  284.                                             {
  285.                                                 for (int asciicountersix7digit = 48; asciicountersix7digit <= 122; asciicountersix7digit++)
  286.                                                 {
  287.                                                     bruteforce[arrayvaluesix7digit] = (char)asciicountersix7digit;
  288.                                                    
  289.                                                     for (int arrayvalueseven7digit = 6; arrayvalueseven7digit < 7; arrayvalueseven7digit++)
  290.                                                     {
  291.                                                         for (int asciicounterseven7digit = 48; asciicounterseven7digit <= 122; asciicounterseven7digit++)
  292.                                                         {
  293.                                                             bruteforce[arrayvalueseven7digit] = (char)asciicounterseven7digit;
  294.                                                    
  295.                                                             crackedpassword = crypt(bruteforce, searchsalt);
  296.                                                             check = strcmp(crackedpassword, givenhash);
  297.                                                             if (check == 0)
  298.                                                             {
  299.                                                                 printf("Username: %s \nEncrypted password: %s \nPassword: %s \n", username, givenhash, bruteforce);
  300.                                                                 return 0;
  301.                                                             }
  302.                                                         }
  303.                                                     }
  304.                                                 }
  305.                                             }
  306.                                         }
  307.                                     }
  308.                                 }
  309.                             }
  310.                         }
  311.                     }
  312.                 }
  313.             }
  314.         }
  315.     }
  316.    
  317.     // Brute force check eight digit strings
  318.     for (int arrayvalueone8digit = 0; arrayvalueone8digit < 1; arrayvalueone8digit++)
  319.     {
  320.         for (int asciicounterone8digit = 48; asciicounterone8digit <= 122; asciicounterone8digit++)
  321.         {
  322.             bruteforce[arrayvalueone8digit] = (char)asciicounterone8digit;
  323.            
  324.             for (int arrayvaluetwo8digit = 1; arrayvaluetwo8digit < 2; arrayvaluetwo8digit++)
  325.             {
  326.                 for (int asciicountertwo8digit = 48; asciicountertwo8digit <= 122; asciicountertwo8digit++)
  327.                 {
  328.                     bruteforce[arrayvaluetwo8digit] = (char)asciicountertwo8digit;
  329.                    
  330.                     for (int arrayvaluethree8digit = 2; arrayvaluethree8digit < 3; arrayvaluethree8digit++)
  331.                     {
  332.                         for (int asciicounterthree8digit = 48; asciicounterthree8digit <= 122; asciicounterthree8digit++)
  333.                         {
  334.                             bruteforce[arrayvaluethree8digit] = (char)asciicounterthree8digit;
  335.                            
  336.                             for (int arrayvaluefour8digit = 3; arrayvaluefour8digit < 4; arrayvaluefour8digit++)
  337.                             {
  338.                                 for (int asciicounterfour8digit = 48; asciicounterfour8digit <= 122; asciicounterfour8digit++)
  339.                                 {
  340.                                     bruteforce[arrayvaluefour8digit] = (char)asciicounterfour8digit;
  341.                                    
  342.                                     for (int arrayvaluefive8digit = 4; arrayvaluefive8digit < 5; arrayvaluefive8digit++)
  343.                                     {
  344.                                         for (int asciicounterfive8digit = 48; asciicounterfive8digit <= 122; asciicounterfive8digit++)
  345.                                         {
  346.                                             bruteforce[arrayvaluefive8digit] = (char)asciicounterfive8digit;
  347.                                            
  348.                                             for (int arrayvaluesix8digit = 5; arrayvaluesix8digit < 6; arrayvaluesix8digit++)
  349.                                             {
  350.                                                 for (int asciicountersix8digit = 48; asciicountersix8digit <= 122; asciicountersix8digit++)
  351.                                                 {
  352.                                                     bruteforce[arrayvaluesix8digit] = (char)asciicountersix8digit;
  353.                                                    
  354.                                                     for (int arrayvalueseven8digit = 6; arrayvalueseven8digit < 7; arrayvalueseven8digit++)
  355.                                                     {
  356.                                                         for (int asciicounterseven8digit = 48; asciicounterseven8digit <= 122; asciicounterseven8digit++)
  357.                                                         {
  358.                                                             bruteforce[arrayvalueseven8digit] = (char)asciicounterseven8digit;
  359.                                                            
  360.                                                             for (int arrayvalueeight8digit = 7; arrayvalueeight8digit < 8; arrayvalueeight8digit++)
  361.                                                             {
  362.                                                                 for (int asciicountereight8digit = 48; asciicountereight8digit <= 122; asciicountereight8digit++)
  363.                                                                 {
  364.                                                                     bruteforce[arrayvalueeight8digit] = (char)asciicountereight8digit;
  365.                                                            
  366.                                                                     crackedpassword = crypt(bruteforce, searchsalt);
  367.                                                                     check = strcmp(crackedpassword, givenhash);
  368.                                                                     if (check == 0)
  369.                                                                     {
  370.                                                                         printf("Username: %s \nEncrypted password: %s \nPassword: %s \n", username, givenhash, bruteforce);
  371.                                                                         return 0;
  372.                                                                     }
  373.                                                                 }
  374.                                                             }
  375.                                                         }
  376.                                                     }
  377.                                                 }
  378.                                             }
  379.                                         }
  380.                                     }
  381.                                 }
  382.                             }
  383.                         }
  384.                     }
  385.                 }
  386.             }
  387.         }
  388.     }
  389.  
  390.     return 0;
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement