Zach
Zach

Reputation: 11

Basic Brute Force Program in C

I'm working on a program that compares an encrypted passcode to test words encrypted with the same Salt value. A Salt is a 2 character password used to in the crypt() function to further convolute input, and is saved as the first 2 digits of the encrypted password. Intended result is meant to be the password/Salt combination that is encrypted to the same value as the password I'm looking for-- an equivalent password. However, the Salt does not print properly, as it comes up with additional (unreadable) information after the 2 characters read from the encrypted password. Though no errors come up when the program is run, the it comes up with the same results no matter what passcode is used, and 2 warnings come up upon compiling:

Crack1.c:54:27: warning: implicit declaration of function 'crypt' is invalid    in
      C99 [-Wimplicit-function-declaration]
            *decyphered = crypt(wordtest, salt);
                          ^
Crack1.c:60:53: warning: comparison of array 'wordtest' not equal to a null
      pointer is always true [-Wtautological-pointer-compare]
        while(!memcmp(enc1, decyphered, (k + 2)) && wordtest != NULL);
                                                ^~~~~~~~    ~~~~
2 warnings generated.

Upon running:

Test

Print encrypted text: 50zPJlUFIYY0o
Encrypted text: 50zPJlUFIYY0o
Salt value #0 = 5
Salt value #1 = 0
Full Salt: 50?Z?V?
Password: aA

Password: A&A

Password: A&NV

Password: A&NXM

Password: 


Password: A&DwSOD

Password: A&BQVANT

I'm guessing that the salt issue is affecting the crypt() function.

Source code:

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

/*
This program take an encrypted input
before encrypting a host of strings
and comparing those encryptions to
the original input. All files read
from are to be kept in the same
folder.
*/

int main(int argc, char *argv[])
{

    //Get and print text to be encyphered
    printf("\nTest\n");
    char enc1[50];
    char salt[2];
    char decyphered[50];
    char filename[8][15] = {"two.txt","three.txt",         "four.txt","five.txt","six.txt","seven.txt","eight.txt"};

    char wordtest[10];
    printf("\nPrint encrypted text: ");
    scanf("%s", enc1);
    printf("Encrypted text: %s\n", enc1);

    //Fill salt
    for (int i = 0; i < 2; i++)
    {
        salt[i] = enc1[i];
        printf("Salt value #%i = %c\n", i, salt[i]);
    }

    printf("Full Salt: %s", salt);

    //Run all possibilites and check result
    FILE *fp;

    for (int k = 0; k < 8; k++)
    {
        fp = fopen(filename[k],"r");  // opens files in read mode

        if(fp == NULL)
        {
           perror("\nError while opening the file.\n");
           exit(EXIT_FAILURE);
        }

        do
        {
            fgets(wordtest, k + 3, (FILE*)fp);
            *decyphered = crypt(wordtest, salt);
            if (memcmp(enc1, decyphered, (k + 2)))
            {
                printf("\nPassword: %s\n", wordtest);
            }
        }
        while(!memcmp(enc1, decyphered, (k + 2)) && wordtest != NULL);


    }

    return 0;
}

Any help would be greatly appreciated!

Upvotes: 0

Views: 2251

Answers (1)

Pooya
Pooya

Reputation: 6126

The shortest solution to the salt problem is:

printf("Full Salt: %c%c", salt[0],salt[1]);

or you can define salt[3] and set salt[2] = '\0' then use your printf to print with %s

Regarding wordtest != NULL warning, when you allocate wordtest as char[10] then you already allocated space in stack to that variable so comparing it to null is not rational but probably you wanted to test if the content of wordtest is empty or not which can be done with:

strcmp(wordtest, "");

Upvotes: 0

Related Questions