Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:29:05+00:00 2026-05-27T14:29:05+00:00

I’m doing an exercise. The goal is to make a program in C to

  • 0

I’m doing an exercise. The goal is to make a program in C to crack DES encrypted password.
Right now I have the following flow of execution:

  1. Load dictionary.
  2. Dictionary search.
  3. Brute force search of the first 4 characters.
  4. Dictionary search combined with brute force (searching for
    combinations). Only dictionary words of 7-6 characters.
  5. Brute force search of the first 5 characters.
  6. Dictionary search combined with brute force (searching for
    combinations). Only dictionary words of 5-4 characters.
  7. Brute force search of up to 8 characters.

The program works fine, but I want to improve it by using multiple
threads:
1st thread – main
2nd thread – dictionary and dictionary combined with brute force
search
3rd thread – brute force search

I’ve started by making a basic dictionary search thread function, but
it fails with Bus Error (Mac OS X) where it should start reading words
from the dictionary file. Same code works fine in the regular non-
thread function…

Here is the code:

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define _XOPEN_SOURCE
#define MAXLINE 40
#define MAXPASS 9

/* dictionary search thread function */
void * dictionary(void * argv)
{
    /* initializing SALT */
    char salt[3];               // defining salt (length is always 2 chars + "\0")
    strncpy(salt, argv, 2);     // copying the first 2 characters from encrypted password to salt
    salt[2] = '\0';             // placing null character to make salt a string

    /* defining and initializing password */
    char password[14];
    strcpy(password, argv);
    /* defining candidate */
    char  candidate[MAXPASS];

    /* opening file */
    FILE *fp;
    if ((fp = fopen("/usr/share/dict/words", "r")) == NULL)
    {
        printf("Error: Can not open file.\n");
        return (void *) -1;
    }
    printf("Open file: Ok\n");
    char line[MAXLINE];
    printf("Counting words: "); 
    /* counting words the file contains */
    int ctr = 0;    // words counter variable 
    int len;        // store length of the current line
    while (fgets(line, MAXLINE, fp) != NULL && line[0] != '\n')
    {
        if ((len = strlen(line)) <= MAXPASS && len >= 4)
            ctr++;  // will be real+1 when the loop ends
    }
    ctr--;          // adjusting to real words count
    rewind(fp);     // go back to the beginning of file
    printf("%d words\n", ctr);

    /* create an array of strings and fill it with the words from the dictionary */
    printf("Creating array for file contents: ");
    char words[ctr][MAXPASS];
    int i = 0;      // loop counter variable
    printf("Ok\n");
    /************************************* BUS ERROR *********************************************/
    printf("Reading file contents: ");
    while (fgets(line, MAXLINE, fp) != NULL && line[0] != '\n')
    {
        if ((len = strlen(line)) <= MAXPASS && len >= 4)
        {
            line[len-1] = '\0';
            strcpy(words[i], line);
            printf("%d: %s\n", i, words[i]);
            i++;
        }
    }
    printf("Ok\n");
    printf("Loaded %d words...\n", ctr);

    /* closing file */
    printf("Close file: ");
    if (fclose(fp) != 0)
    {
        fprintf(stderr, "Error: Can not close file\n");
        return (void *) -2;
    }
    printf("Ok\n");

    /* starting search dictionary search */
    printf("Starting Dictionary Search...\n");
    int match = 0;
    char * encrypted;
    int n;
    for (i = 0; i <= ctr && !match; i++)
    {
        encrypted = crypt(words[i], salt);
        if ((strcmp(encrypted, password)) == 0)             // if candidate == password
        {
            match = 1;
            strcpy(candidate, words[i]);
            printf("Password: %s\n", candidate);
            return (void *) 1;
        }
    }

    return (void *) 0;
}
int main(int argc, char * argv[])
{
    /* if there are less/more than 1 argument, notify the user and exit with an error code  1 */
    if (argc != 2)      // first argument is always the name of the program
    {
        printf("Error 1: Wrong number of arguments\n");             
        return 1;
    }
    /* if the length of the argument is less/more than 13 characters, notify the user and exit with an error code 2 */
    int length = strlen(argv[1]);
    if (length != 13)
    {
        printf("Error 2: The length of an encrypted password should be 13 characters\n");
        return 2;
    }

    pthread_t dct;      // dictionary thread identifier
    void *status;       // thread return value

    /* creating dictionary thread */
    pthread_create(&dct,NULL,dictionary,argv[1]);

    printf("Waiting for thread to terminate...\n");
    pthread_join(dct,&status);

    //printf("Return Value: %d\n",(int)status);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:29:05+00:00Added an answer on May 27, 2026 at 2:29 pm

    I’m going to guess that this is your problem:

    char words[ctr][MAXPASS];
    

    When you’re running a single-threaded program, you’ve got plenty of address space for the stack to grow down, libraries and program executable space to grow up, and heap in the middle.

    But when you’re running multi-threaded programs, each thread gets its own stack and I wouldn’t be surprised if the stack space available for the threads is significantly smaller than your dictionary size. (See the pthread_attr_getstack() manpage on your system for details on the per-thread stack size default.)

    Allocate that array with malloc(3) and see if your program gets further.

    char *words;
    words = malloc(ctr * sizeof(char));
    int i; // loop counter variable
    for (i = ; i < ctr; i++)
        words[i] = malloc(MAXPASS * sizeof(char));
    

    If you find the multiple malloc(3) calls introduce enough memory fragmentation, you can use some slightly-gross casting to allocate a single large block of memory and treat it identically to the multidimensional array:

    $ cat multidimensional.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NWORDS 1000
    #define WORDLEN 10
    
    void fun(char words[NWORDS][WORDLEN]) {
        int i, j;
        for (i=0; i<NWORDS; i++) {
            strcpy(words[i], "test");
        }
    
        for (i=0; i<NWORDS; i++) {
            printf("%s\n", words[i]);
        }
        return;
    }
    
    
    int main(int argc, char* argv[]) {
        char *w = malloc(NWORDS * WORDLEN * sizeof(char));
        memset(w, 0, NWORDS * WORDLEN * sizeof(char));
        fun((char (*)[WORDLEN]) w);
    
        return 0;
    }
    

    You’d have to use another function because you cannot assign to an array but when you write a function that should be passed an array as an argument it actually decays to the pointer cast in the function call: char (*)[WORDLEN]. (It could have been written: void fun(char (*)[WORDLEN]) as well but I don’t think that is as legible.)

    I’m always a little worried when I silence a warning with a cast, as I’ve done here, but this does perform a single large allocation instead of thousands of little tiny allocations, which might a huge performance difference. (Test both and see.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.