Brute force works for a long time as you can speed up the execution of the program

Password-protected archives are given. The archives were created by the 7z program.

#include <ctime>
#include <fstream>
#include <string>

#define MAX_SIZE 30
#define CHAR_COUNT 36

using namespace std;

string password;

void bruteForce(string& name) {
    cout << "Brute force started...\n";
    clock_t tStart = clock();
    const char chars[CHAR_COUNT + 1] = "abcdefghijklmnopqrstuvwxyz0123456789";
    int i, j;
    int total = 0;
    int guessc[MAX_SIZE] = { 0 };
    char guess[MAX_SIZE + 1];
    string address = "\"\"C:\\Program Files\\7-Zip\\7z.exe\" t -bso0 -bse0 -p";

    for (i = 1; i < MAX_SIZE; guessc[i++] = -1);
    for (i = 1; i <= MAX_SIZE; guess[i++] = '\0');

    while (true) {
        i = 0;
        while (guessc[i] == CHAR_COUNT) {
            guessc[i] = 0;
            guessc[++i] += 1;
        }

        for (j = 0; j <= i; ++j)
            if (j < MAX_SIZE)
                guess[j] = chars[guessc[j]];

        address.append(guess);
        address.append(" ");
        address.append(name);
        address.append("\" > nul");
        if (system(address.c_str()) == 0) {
            password = guess;
            printf("Brute-force->\nPassword: %s \nTime taken: %.2fs\n", guess, (double)(clock() - tStart) / CLOCKS_PER_SEC);
            break;
        }
        address = "\"\"C:\\Program Files\\7-Zip\\7z.exe\" t -bso0 -bse0 -p";
        ++guessc[0];
        total++;
    }
    cout << "Total iterations: " << total << "\n\n";
}

string dictionary(string& name) {
    cout << "Search in dictionary started...\n";
    clock_t tStart = clock();
    ifstream in("rockyou.txt");
    int total = 0;
    string s, address = "\"\"C:\\Program Files\\7-Zip\\7z.exe\" t -bso0 -bse0 -p";

    while (getline(in, s)) {
        address.append(s);
        address.append(" ");
        address.append(name);
        address.append("\" > nul");
        if (system(address.c_str()) == 0) {
            password = s;
            printf("Dictionary->\nPassword: %s \nTime taken: %.2fs\n", s.c_str(), (double)(clock() - tStart) / CLOCKS_PER_SEC);
            break;
        }
        address = "\"\"C:\\Program Files\\7-Zip\\7z.exe\" t -bso0 -bse0 -p";
        total++;
    }
    cout << "Total iterations: " << total << "\n\n";
    return password;
}


void printFileContent(string& pass, string& name) {

    cout << "Archive content:\n";
    string s, address = "\"\"C:\\Program Files\\7-Zip\\7z.exe\" e -y -bso0 -bse0 -p";
    if (!pass.empty()) address.append(pass);
    address.append(" ");
    address.append(name);
    address.append("\" > nul");
    if (system(address.c_str()) == 0) {
        ifstream in("test.txt");
        while (getline(in, s))
            cout << s << '\n';
    }
}



int main() {

    string file_name = "ki1801.08.7z";
    bruteForce(file_name);
    string pass = dictionary(file_name);
    printFileContent(pass, file_name);

    return 0;
}
Author: Kukarekoo, 2020-12-16