daktarism

Random Password creator & cracking (using the lenght)

Mar 12th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.14 KB | None | 0 0
  1. /**
  2. @file
  3. BBG2 spring2014 assignment 1.
  4.  
  5. An algorithm, which tries to find the correct password generated by the system.
  6. system will either accept a password from the user or generate a random password whose characters consist of
  7.    * only digits
  8.    * only lowercase letters
  9.    * both lowercase letters and digits
  10.    * lowercase letters, digits and uppercase letters
  11.  
  12. Inputs: The password (if necessary)
  13.  The type of password and the type of characters
  14.  
  15. Output: The number of attempts
  16.  The password
  17.  The time duration passed until the password has been guessed correctly.
  18.  
  19.  
  20.  
  21. @author
  22.  
  23. Name: Tolga Ay
  24.  
  25. Student no: 13011057
  26.  
  27. Date: 12.03.2014
  28.  
  29. E-Mail: tolga-ay@outlook.com
  30.  
  31. Compiler used: GCC
  32.  
  33. IDE: Code::Blocks 13.12
  34.  
  35. Operating System : Windows 7
  36. */
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <math.h>
  41. #include <time.h>
  42. #include <stdlib.h>
  43.  
  44. //functions I will use
  45. int random(int x,int y){return (rand()%(y-x+1))+x;} // x<= RASTGELE SAYI <=y araliginda rastgele sayi üretiyor
  46. void createPassword();
  47. void findThePassword();
  48.  
  49. //global degiskenler
  50. char selection,password[150],generatedPassword[150];
  51. int passLenght,randomType,i,k,attempt=0,realLenght;
  52. clock_t start_t, end_t, total_t; //sayaç degiskenleri
  53.  
  54. /**
  55.  Main function. Kullaniciya bir sifre olusturtuyor, sonra ana menuye gecip yeni sifre olusturma veya sifreyi bulma secenekleri sunuyor.
  56.  her sona gelindiginde ana menuye donuyor.
  57. */
  58.  
  59.  
  60. int main(){
  61.     createPassword(); // sifre olusturuluyor
  62.  
  63.     while(1){// programin bitmemesini sagliyor, her sona gelindiginde ana menüye dönüyor
  64.     system("cls"); // ekran temizleniyor
  65.  
  66.     //ana menü
  67.     printf("Main Menu:\n1-) Create a new password\n2-) Try to find the correct password\n\nSelection : ");
  68.  
  69.     //seçim aliniyor fakat 1 veya 2 disinda herhangi bir karakter girilemez.
  70.     do{
  71.     scanf("%c",&selection);
  72.     }while(selection != '1' && selection != '2');
  73.  
  74.     system("cls"); // ekran temizleniyor
  75.  
  76.     //seçim yapildiktan sonra ekran temizlenip seçime göre yeni sifre yaratma veya sifreyi bulma kisimlarina geçiliyor
  77.  
  78.     if(selection == '2')
  79.         findThePassword(); //SIFREYI BULMA BÖLÜMÜ (ANA MENÜ SELECTION 2)
  80.     else
  81.         createPassword(); //YENI SIFRE OLUSTURMA KISMI (ANA MENÜ SELECTION 1)
  82.  
  83.     // buraya gelince ana menü kismina dönülüyor
  84.     }
  85. }
  86.  
  87.  
  88.  
  89.  
  90. /**
  91.  Password creating function. Kullanicidan sifre aliyor veya kullanicinin tercihlerine gore rastgele bir sifre olusturuyor.
  92. */
  93.  
  94.  
  95. void createPassword(){
  96.     srand(time(NULL)); //random komutunun her seferinde farkli sonuc vermesini sagliyor
  97.  
  98.     printf("Create password:\n1-)Password from the user\n2-)Generate a random password\n\nSelection : ");
  99.  
  100.         //seçim aliniyor fakat 1 veya 2 disinda herhangi bir karakter girilemez.
  101.         do{
  102.         scanf("%c",&selection);
  103.         }while(selection != '1' && selection != '2');
  104.  
  105.         system("cls"); //ekran temizleniyor
  106.  
  107.  
  108.         //seçim yapildiktan sonra ekran temizlenip seçime göre sifreyi kullanicidan alma veya rastgele sifre olusturma kisimlarina geçiliyor
  109.  
  110.         //SIFREYI KULLANICIDAN ALMA
  111.         if(selection=='1'){
  112.             printf("Enter a new password : ");
  113.             scanf("%s",password);
  114.             printf("Your Password : %s\n",password);
  115.             realLenght=strlen(password);  // sifrenin uzunlugu sonradan kullanmak için bir degiskene ataniyor
  116.         }
  117.  
  118.         //RASTGELE SIFRE OLUSTURMA
  119.         else{
  120.  
  121.             //sifre boyutu aliniyor
  122.             printf("Enter the lenght of password:");
  123.  
  124.             do{
  125.             scanf("%d",&passLenght);
  126.             }while(passLenght>149 || passLenght<1);
  127.  
  128.  
  129.  
  130.             //karakter tipi seçimi aliniyor fakat 1,2,3 veya 4 disinda herhangi bir karakter girilemez.
  131.             printf("\nEnter the type of characters:\n1-)Only digits\n2-)Only lowcase letters\n3-)Both lowcase letters and digits\n4-)Lowcase letters, digits and uppercase letters\n");
  132.  
  133.             do{
  134.             scanf("%c",&selection);
  135.             }while(selection != '1' && selection != '2' && selection != '3' && selection != '4');
  136.  
  137.             system("cls"); //ekran temizleniyor
  138.  
  139.             //karakter tipi(1,2,3,4) ve sifre uzunlugu(passLenght) seçimine göre rastgele sifre olusturuluyor
  140.             switch(selection){
  141.  
  142.                 case '1': for(i=0;i<passLenght;i++) //her döndügünde sifre 1 karakter 1 karakter olusuyor
  143.                     {password[i]=random(48,57);}break; //rastgele rakam
  144.  
  145.  
  146.                 case '2': for(i=0;i<passLenght;i++) //her döndügünde sifre 1 karakter 1 karakter olusuyor
  147.                     {password[i]=random(97,122);}break; //rastgele küçük harf
  148.  
  149.  
  150.                 case '3': for(i=0;i<passLenght;i++)
  151.                     {randomType=random(1,2); //küçük harfler veya rakamlar diye rastgele bir seçim yapiliyor
  152.                      switch(randomType){
  153.                           case 1:password[i]=random(48,57);break; //sonuca göre rastgele rakam
  154.                           case 2:password[i]=random(97,122);break; //sonuca göre rastgele küçük harf
  155.                                        }
  156.                     }break;
  157.  
  158.  
  159.                 case '4': for(i=0;i<passLenght;i++){ //her döndügünde sifre 1 karakter 1 karakter olusuyor
  160.                                                         randomType=random(1,3); //küçük harfler, büyük harfler, rakamlar diye rastgele bir seçim yapiliyor
  161.                                                         switch(randomType){
  162.                                                             case 1:password[i]=random(48,57);break; //sonuca göre rastgele rakam
  163.                                                             case 2:password[i]=random(97,122);break; //sonuca göre rastgele küçük harf
  164.                                                             case 3:password[i]=random(65,90);break; //sonuca göre rastgele büyük harf
  165.                                                         }
  166.                                                    }
  167.                             }
  168.  
  169.             realLenght=passLenght; // sifrenin uzunlugu sonradan kullanmak için bir degiskene ataniyor
  170.  
  171.             //rastgele olusturulan sifreyi yazdiriyor
  172.             printf("The generated random number : ");
  173.             for(i=0;i<passLenght;i++)
  174.                 printf("%c",password[i]);
  175.  
  176.             getch(); //ekranin okunabilmesi için program bir tus girilene kadar bekliyor
  177. /*programin sonu, buradan ana menüye dönüyor.*/
  178. }
  179.  
  180.  
  181. }
  182.  
  183. /**
  184.  Password finding function. Kullanicinin girdigi veya rastgele olusturdugu sifreyi brute force teknigiyle bulur, ve ekrana cikti verir.
  185. */
  186.  
  187. void findThePassword(){
  188.         int x;
  189.         char digitArray[]={47,48,48,48,48,48,48,48,48,48};
  190.         start_t=clock();
  191.         printf("Cracking the password, please wait.\n\n");
  192.  
  193.         while( strncmp(password,generatedPassword) != 0 ){
  194.         digitArray[0]++;
  195.  
  196.         for(x=0;x<strlen(password);x++){
  197.            if (digitArray[x] == 123)
  198.                 {
  199.                     digitArray[x] = 48;
  200.                     digitArray[x + 1]++;
  201.                 }
  202.         }
  203.  
  204.         for(x=0;x<strlen(password);x++){
  205.            generatedPassword[x]=digitArray[x];
  206.         }
  207.         attempt++;
  208. }
  209.  
  210. //sayaç durduruluyor
  211.         end_t = clock(); // saatin son degeri milisaniye cinsinden
  212.  
  213.         //programin çiktisi
  214.         printf("The number of attempts : %d\nThe password : %s",attempt,generatedPassword);
  215.         printf("\nThe time duration  passed : %.3f  seconds",(double)(end_t - start_t)/1000  ); //geçen süre hesaplaniyor
  216.         getch(); //ekranin okunabilmesi için program bir tus girilene kadar bekliyor
  217.  
  218. /*programin sonu, buradan ana menüye dönüyor.*/
  219.  
  220. }
Add Comment
Please, Sign In to add comment