Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- @file
- BBG2 spring2014 assignment 1.
- An algorithm, which tries to find the correct password generated by the system.
- system will either accept a password from the user or generate a random password whose characters consist of
- * only digits
- * only lowercase letters
- * both lowercase letters and digits
- * lowercase letters, digits and uppercase letters
- Inputs: The password (if necessary)
- The type of password and the type of characters
- Output: The number of attempts
- The password
- The time duration passed until the password has been guessed correctly.
- @author
- Name: Tolga Ay
- Student no: 13011057
- Date: 12.03.2014
- E-Mail: tolga-ay@outlook.com
- Compiler used: GCC
- IDE: Code::Blocks 13.12
- Operating System : Windows 7
- */
- #include <stdio.h>
- #include <math.h>
- #include <time.h>
- #include <stdlib.h>
- //functions I will use
- int random(int x,int y){return (rand()%(y-x+1))+x;} // x<= RASTGELE SAYI <=y araliginda rastgele sayi üretiyor
- void createPassword();
- void findThePassword();
- //global degiskenler
- char selection,password[150],generatedPassword[150];
- int passLenght,randomType,i,k,attempt=0,realLenght;
- clock_t start_t, end_t, total_t; //sayaç degiskenleri
- /**
- Main function. Kullaniciya bir sifre olusturtuyor, sonra ana menuye gecip yeni sifre olusturma veya sifreyi bulma secenekleri sunuyor.
- her sona gelindiginde ana menuye donuyor.
- */
- int main(){
- createPassword(); // sifre olusturuluyor
- while(1){// programin bitmemesini sagliyor, her sona gelindiginde ana menüye dönüyor
- system("cls"); // ekran temizleniyor
- //ana menü
- printf("Main Menu:\n1-) Create a new password\n2-) Try to find the correct password\n\nSelection : ");
- //seçim aliniyor fakat 1 veya 2 disinda herhangi bir karakter girilemez.
- do{
- scanf("%c",&selection);
- }while(selection != '1' && selection != '2');
- system("cls"); // ekran temizleniyor
- //seçim yapildiktan sonra ekran temizlenip seçime göre yeni sifre yaratma veya sifreyi bulma kisimlarina geçiliyor
- if(selection == '2')
- findThePassword(); //SIFREYI BULMA BÖLÜMÜ (ANA MENÜ SELECTION 2)
- else
- createPassword(); //YENI SIFRE OLUSTURMA KISMI (ANA MENÜ SELECTION 1)
- // buraya gelince ana menü kismina dönülüyor
- }
- }
- /**
- Password creating function. Kullanicidan sifre aliyor veya kullanicinin tercihlerine gore rastgele bir sifre olusturuyor.
- */
- void createPassword(){
- srand(time(NULL)); //random komutunun her seferinde farkli sonuc vermesini sagliyor
- printf("Create password:\n1-)Password from the user\n2-)Generate a random password\n\nSelection : ");
- //seçim aliniyor fakat 1 veya 2 disinda herhangi bir karakter girilemez.
- do{
- scanf("%c",&selection);
- }while(selection != '1' && selection != '2');
- system("cls"); //ekran temizleniyor
- //seçim yapildiktan sonra ekran temizlenip seçime göre sifreyi kullanicidan alma veya rastgele sifre olusturma kisimlarina geçiliyor
- //SIFREYI KULLANICIDAN ALMA
- if(selection=='1'){
- printf("Enter a new password : ");
- scanf("%s",password);
- printf("Your Password : %s\n",password);
- realLenght=strlen(password); // sifrenin uzunlugu sonradan kullanmak için bir degiskene ataniyor
- }
- //RASTGELE SIFRE OLUSTURMA
- else{
- //sifre boyutu aliniyor
- printf("Enter the lenght of password:");
- do{
- scanf("%d",&passLenght);
- }while(passLenght>149 || passLenght<1);
- //karakter tipi seçimi aliniyor fakat 1,2,3 veya 4 disinda herhangi bir karakter girilemez.
- 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");
- do{
- scanf("%c",&selection);
- }while(selection != '1' && selection != '2' && selection != '3' && selection != '4');
- system("cls"); //ekran temizleniyor
- //karakter tipi(1,2,3,4) ve sifre uzunlugu(passLenght) seçimine göre rastgele sifre olusturuluyor
- switch(selection){
- case '1': for(i=0;i<passLenght;i++) //her döndügünde sifre 1 karakter 1 karakter olusuyor
- {password[i]=random(48,57);}break; //rastgele rakam
- case '2': for(i=0;i<passLenght;i++) //her döndügünde sifre 1 karakter 1 karakter olusuyor
- {password[i]=random(97,122);}break; //rastgele küçük harf
- case '3': for(i=0;i<passLenght;i++)
- {randomType=random(1,2); //küçük harfler veya rakamlar diye rastgele bir seçim yapiliyor
- switch(randomType){
- case 1:password[i]=random(48,57);break; //sonuca göre rastgele rakam
- case 2:password[i]=random(97,122);break; //sonuca göre rastgele küçük harf
- }
- }break;
- case '4': for(i=0;i<passLenght;i++){ //her döndügünde sifre 1 karakter 1 karakter olusuyor
- randomType=random(1,3); //küçük harfler, büyük harfler, rakamlar diye rastgele bir seçim yapiliyor
- switch(randomType){
- case 1:password[i]=random(48,57);break; //sonuca göre rastgele rakam
- case 2:password[i]=random(97,122);break; //sonuca göre rastgele küçük harf
- case 3:password[i]=random(65,90);break; //sonuca göre rastgele büyük harf
- }
- }
- }
- realLenght=passLenght; // sifrenin uzunlugu sonradan kullanmak için bir degiskene ataniyor
- //rastgele olusturulan sifreyi yazdiriyor
- printf("The generated random number : ");
- for(i=0;i<passLenght;i++)
- printf("%c",password[i]);
- getch(); //ekranin okunabilmesi için program bir tus girilene kadar bekliyor
- /*programin sonu, buradan ana menüye dönüyor.*/
- }
- }
- /**
- Password finding function. Kullanicinin girdigi veya rastgele olusturdugu sifreyi brute force teknigiyle bulur, ve ekrana cikti verir.
- */
- void findThePassword(){
- int x;
- char digitArray[]={47,48,48,48,48,48,48,48,48,48};
- start_t=clock();
- printf("Cracking the password, please wait.\n\n");
- while( strncmp(password,generatedPassword) != 0 ){
- digitArray[0]++;
- for(x=0;x<strlen(password);x++){
- if (digitArray[x] == 123)
- {
- digitArray[x] = 48;
- digitArray[x + 1]++;
- }
- }
- for(x=0;x<strlen(password);x++){
- generatedPassword[x]=digitArray[x];
- }
- attempt++;
- }
- //sayaç durduruluyor
- end_t = clock(); // saatin son degeri milisaniye cinsinden
- //programin çiktisi
- printf("The number of attempts : %d\nThe password : %s",attempt,generatedPassword);
- printf("\nThe time duration passed : %.3f seconds",(double)(end_t - start_t)/1000 ); //geçen süre hesaplaniyor
- getch(); //ekranin okunabilmesi için program bir tus girilene kadar bekliyor
- /*programin sonu, buradan ana menüye dönüyor.*/
- }
Add Comment
Please, Sign In to add comment