You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于DES的crypt()暴力破解程序无限循环排查及替代方案咨询

Hey there! Let's dig into your infinite loop problem and talk about cleaner alternatives to nested loops too.

排查无限循环的常见原因

First, let's walk through the most likely culprits for your infinite loop—start checking these areas first:

  • Incorrect loop boundary conditions
    If you're using nested loops for each password character, it's easy to mess up the start/end values. For example, if you wrote while (c < 'z') instead of while (c <= 'z'), you'll miss the last character and might accidentally create a loop that never terminates. Double-check each loop's logic to ensure it runs exactly 26 times (once for each lowercase letter).
  • Broken carry-over logic
    When the last character hits 'z', you need to reset it to 'a' and increment the previous character (and repeat this carry-over for all preceding characters if they also hit 'z'). If this logic is missing or buggy, your program might get stuck looping on one character position forever. For example, if the 5th character keeps resetting but the 4th never increments, you'll loop through all 5th-letter combinations infinitely.
  • Hash matching failure
    If your comparison logic is wrong, the program will never trigger the exit condition. Common mistakes here include:
    • Using != instead of == in the hash check
    • Forgetting that DES-based crypt() uses the first two characters of the target hash as the salt. Make sure you're calling crypt(guess_password, target_hash)—this way the function automatically uses the correct salt. If you hardcode a wrong salt, your guesses will never match, leading to an infinite loop.
Alternatives to Nested Loops

Nested loops for 5 characters get messy fast. Here are two cleaner approaches:

1. Base-26 Conversion (Single Loop)

Treat each password as a base-26 number (where 0 = 'a', 1 = 'b', ..., 25 = 'z'). This lets you iterate through all possible combinations with a single loop, no nested logic needed:

#include <stdio.h>
#include <crypt.h>
#include <string.h>

int main() {
    const char* target_hash = "your_target_hash_here";
    char guess[6]; // 5 chars + null terminator
    const long long total_combinations = 26LL * 26 * 26 * 26 * 26; // 26^5

    for (long long i = 0; i < total_combinations; i++) {
        long long num = i;
        // Convert number to 5-letter password
        for (int j = 4; j >= 0; j--) {
            guess[j] = 'a' + (num % 26);
            num /= 26;
        }
        guess[5] = '\0';

        // Compare hashes
        char* result = crypt(guess, target_hash);
        if (strcmp(result, target_hash) == 0) {
            printf("Found password: %s\n", guess);
            return 0;
        }
    }
    printf("Password not found.\n");
    return 1;
}

2. Recursive Password Generation

Use recursion to build passwords character by character. This avoids manual carry-over logic and keeps the code structured:

#include <stdio.h>
#include <crypt.h>
#include <string.h>

const char* target_hash = "your_target_hash_here";
int password_found = 0;

void brute_force(char* guess, int current_pos) {
    if (password_found) return; // Exit early if we found it

    if (current_pos == 5) {
        guess[5] = '\0';
        char* result = crypt(guess, target_hash);
        if (strcmp(result, target_hash) == 0) {
            printf("Found password: %s\n", guess);
            password_found = 1;
        }
        return;
    }

    // Try every lowercase letter for the current position
    for (char c = 'a'; c <= 'z'; c++) {
        guess[current_pos] = c;
        brute_force(guess, current_pos + 1);
    }
}

int main() {
    char guess[6];
    brute_force(guess, 0);
    
    if (!password_found) {
        printf("Password not found.\n");
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者mrwnt10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:52:54