It is required to write your Bruteforce, i.e. the user enters a password and the program will search for this password by searching all possible options. It is assumed that the program can not work and not find the password. Restriction of search is carried out by the user, i.e. whether numbers, capital letters, symbols, etc. will be included in the search.

I recommend limiting the length of the entered password to 4 characters, no longer necessary, otherwise the program will work for a long time. Even better, during the development phase of the program, the password length will be 2 - 3 characters. In addition, specify a set of valid password characters in the program. For example, only numbers and / or letters can be used in a password, this will markedly help speed up the process of debugging a brute-force program. The task is taken from http://cppstudio.com/post/8059/comment-page-4/#comment-3414

I wanted to know if I could somehow cut my code?

/* * by biggy * * Bruteforce */ #include <stdio.h> #include <stdlib.h> #include <string.h> void yyy(void); void nyy(void); void nny(void); void yny(void); void yyn(void); int main (void) { char y1 = 0, y2 = 0, y3 = 0; printf("Will you use characters (y/n) ?"); scanf(" %c", &y1); printf("Will you use big characters (y/n) ?"); scanf(" %c", &y2); printf("Will you use numbers (y/n) ?"); scanf(" %c", &y3); if(y1 == 'y' && y2 == 'y' && y3 == 'y') yyy(); else if(y1 == 'n' && y2 == 'y' && y3 == 'y') nyy(); else if(y1 == 'n' && y2 == 'n' && y3 == 'y') nny(); else if(y1 == 'y' && y2 == 'n' && y3 == 'y') yny(); else if(y1 == 'y' && y2 == 'y' && y3 == 'n') yyn(); printf("done."); return EXIT_SUCCESS; } void yyy(void) { char buffer[5] = {0}; char buffer2[5] = {0}; printf("Enter your password ?"); scanf("%4s", buffer); for(char i = '0'; i != '}'; ++i) { for(char j = '0'; j != '}'; ++j) { for(char u = '0'; u != '}'; ++u) { for(char z = '0'; z != '}'; ++z) { if(i == ':') i = 'a'; else if(i == '{') i = 'A'; if(j == ':') j = 'a'; else if(j == '{') j = 'A'; if(u == ':') u = 'a'; else if(u == '{') u = 'A'; if(z == ':') z = 'a'; else if(z == '{') z = 'A'; buffer2[0] = i; buffer2[1] = j; buffer2[2] = u; buffer2[3] = z; //printf("%s\r\n", buffer2); if(strcmp(buffer, buffer2) == 0) printf("Your password: \t%s\r\n", buffer2); if(z == 'Z') break; } if(u == 'Z') break; } if(j == 'Z') break; } if(i == 'Z') break; } return; } void nyy(void) { char buffer[5] = {0}; char buffer2[5] = {0}; printf("Enter your password ?"); scanf("%4s", buffer); for(char i = '0'; i != '}'; ++i) { for(char j = '0'; j != '}'; ++j) { for(char u = '0'; u != '}'; ++u) { for(char z = '0'; z != '}'; ++z) { if(i == ':') i = 'A'; else if(j == ':') j = 'A'; else if(u == ':') u = 'A'; else if(z == ':') z = 'A'; buffer2[0] = i; buffer2[1] = j; buffer2[2] = u; buffer2[3] = z; //printf("%s\r\n", buffer2); if(strcmp(buffer, buffer2) == 0) printf("Your password: \t%s\r\n", buffer2); if(z == 'Z') break; } if(u == 'Z') break; } if(j == 'Z') break; } if(i == 'Z') break; } return; } void nny(void) { char buffer[5] = {0}; char buffer2[5] = {0}; printf("Enter your password ?"); scanf("%4s", buffer); for(int i = 48; i < 58; ++i) { for(int j = 48; j < 58; ++j) { for(int u = 48; u < 58; ++u) { for(int z = 48; z < 58; ++z) { buffer2[0] = i; buffer2[1] = j; buffer2[2] = u; buffer2[3] = z; //printf("%s\r\n", buffer2); if(strcmp(buffer, buffer2) == 0) printf("Your password: \t%s\r\n", buffer2); } } } } return; } void yny(void) { char buffer[5] = {0}; char buffer2[5] = {0}; printf("Enter your password ?"); scanf("%4s", buffer); for(char i = '0'; i != '}'; ++i) { for(char j = '0'; j != '}'; ++j) { for(char u = '0'; u != '}'; ++u) { for(char z = '0'; z != '}'; ++z) { if(i == ':') i = 'a'; else if(j == ':') j = 'a'; else if(u == ':') u = 'a'; else if(z == ':') z = 'a'; buffer2[0] = i; buffer2[1] = j; buffer2[2] = u; buffer2[3] = z; //printf("%s\r\n", buffer2); if(strcmp(buffer, buffer2) == 0) printf("Your password: \t%s\r\n", buffer2); if(z == 'z') break; } if(u == 'z') break; } if(j == 'z') break; } if(i == 'z') break; } return; } void yyn(void) { char buffer[5] = {0}; char buffer2[5] = {0}; printf("Enter your password ?"); scanf("%4s", buffer); for(char i = 'a'; i != '}'; ++i) { for(char j = 'a'; j != '}'; ++j) { for(char u = 'a'; u != '}'; ++u) { for(char z = 'a'; z != '}'; ++z) { if(i == 'z') i = 'A'; else if(j == 'z') j = 'A'; else if(u == 'z') u = 'A'; else if(z == 'z') z = 'A'; buffer2[0] = i; buffer2[1] = j; buffer2[2] = u; buffer2[3] = z; printf("%s\r\n", buffer2); /* if(strcmp(buffer, buffer2) == 0) printf("Your password: \t%s\r\n", buffer2); */ if(z == 'Z') break; } if(u == 'Z') break; } if(j == 'Z') break; } if(i == 'Z') break; } return; } 
  • please describe in words what your code does? - Mikhail Vaysman
  • Please describe the algorithm used for coding. If possible, give a link to the standard. add this information to the question. - Mikhail Vaysman
  • Рекомендую ограничить длину вводимого пароля - who recommends it to whom? It looks as if you copied here what the teacher wrote to you. And this whole paragraph - on which data it is better to debug the code - has nothing to do with the question. - Nick Volynkin
  • one
    Worthy occupation. No kidding! My programming is not a profession either :) - Harry

1 answer 1

Yes, easily :) - for example, as shown below. I did not use any of the decent generation algorithms for all tuples, simply because we have only 4 cycles and no more, so I just made them nested ...

 #include <stdio.h> #include <stdlib.h> #include <string.h> char * alpha = "abcdefghijklmnopqrstuvwxyz"; char * bigAl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char * digit = "0123456789"; int main (void) { char y = 0; char syms[128] = {0}; int found = 0; char passw[5] = { 0 }; char guess[5] = { 0 }; printf("Will you use characters (y/n): "); scanf(" %c", &y); if (y=='y') strcat(syms,alpha); printf("Will you use big characters (y/n): "); scanf(" %c", &y); if (y=='y') strcat(syms,bigAl); printf("Will you use numbers (y/n): "); scanf(" %c", &y); if (y=='y') strcat(syms,digit); if (strlen(syms)) { printf("Enter your password: "); scanf("%4s", passw); for(char * c = syms; !found && *c; ++c) { guess[0] = *c; guess[1] = 0; if (strcmp(guess,passw) == 0) { found = 1; break; } for(char * c1 = syms; !found && *c1; ++c1) { guess[1] = *c1; guess[2] = 0; if (strcmp(guess,passw) == 0) { found = 1; break; } for(char * c2 = syms; !found && *c2; ++c2) { guess[2] = *c2; guess[3] = 0; if (strcmp(guess,passw) == 0) { found = 1; break; } for(char * c3 = syms; !found && *c3; ++c3) { guess[3] = *c3; if (strcmp(guess,passw) == 0) { found = 1; break; } } } } } } printf("Password = [%s]\n",(found) ? guess : "NOT FOUND"); printf("done.\n"); return EXIT_SUCCESS; } 

By the way, for the 4 characters, in the worst case, less than 15 million characters move, so I don’t see any reason to find out if there are large letters or not, for example - such a number will still be checked on a modern car almost instantly ...

  • I know. But tried to do on the instructions - biggy
  • Nevertheless thank you! - biggy
  • one
    How does it break? What does it mean? Does not find? Did you answer y to use both letters and capital letters? See for yourself: ideone.com/4xHyu2 - Harry
  • one
    When you write something like - By the way, going through only large or small characters does not work according to your code. let's give a specific example. This is entered, this is received. - Harry
  • one
    Once again - look ideone.com/4xHyu2 You can see for yourself that the password is found. Did you accurately transfer the code and give the correct input? Especially since JUST done , the program simply cannot display without an answer ! Password = line Password = simply obliged to be present - see the code yourself ... - Harry