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; }
Рекомендую ограничить длину вводимого пароля- 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 ♦