密码猜测者,为什么我的密码猜测与实际的哈希密码不相似?

时间:2018-11-06 15:05:32

标签: c encryption crypt

我一直在研究密码猜测程序,现在我一直在试图弄清楚如何让它猜测哈希的密码。我的问题是我猜出的密码的输出与哈希密码没有任何相似之处。我想知道是否有人可以指出我正确的方向。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#include <sys/random.h>

#define BUFFERSIZE 10000

const char saltchars[]="abcdefghikjlmnopqrstuvWxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+@£[]}";
static const int saltcharsSize = sizeof(saltchars) -1;
int counter = 100;
char saltCopy[13];
char passwordCopy[22];

我初始化一个字母,以及一些函数的全局变量。

void crack(char* string, int index, int maxSize) {
    for (int i = 0; i < saltcharsSize; ++i) {
        char* guessPassword = crypt(string, saltCopy);

        if(strcmp(guessPassword, passwordCopy) == 0) {
            printf("Found password: %s\n", string);
        }
        string[index] = saltchars[i];

        if (index == maxSize -1) {
            printf("%s\n", guessPassword);
        }
        else crack(string, index + 1, maxSize);
    }
}

void crackRecursive(int maxLength) {
    char* buffer = malloc(maxLength + 1);

    for (int i = 1; i <= maxLength; ++i) {
        memset(buffer, 0, maxLength + 1);
        crack(buffer, 0, i);
    }
    free(buffer);
}

在这里,我尝试要求它使用相同的盐来加密密码,并将其与副本进行比较。

void readDictionary(char* file) {
    FILE *filePointer = fopen(file, "r");

    char buffer[BUFFERSIZE];

    while(fgets(buffer, BUFFERSIZE -1, filePointer) != NULL && counter != 0) {
        char* guessPasswordNew = crypt(buffer, saltCopy);

        if(strcmp(guessPasswordNew, passwordCopy) == 0){
            printf("found password: %s\n", buffer);
        }
        counter--;
    }
    fclose(filePointer);
    free(filePointer);
    printf("\nEnd of program\n");
}

在这里,我阅读了词典中的文件,并尝试猜测它是否是文件中的单词之一。此处猜测密码的步骤相同。

int cryptPassword(char* password){
    char salt[13]="$1$abcdefgh$";
    getrandom(salt+3,8,0);
    for(unsigned int i=3;i<11;i++){
        salt[i]=saltchars[salt[i] & 0x3f];
    }
    printf("password is: %s\n", password);
    printf("salt is: %s\n", salt);
    strcpy(saltCopy, salt);
    char* encrypted=crypt(password,salt);
    strcpy(passwordCopy, encrypted);
    printf("hashed password is: %s\n", encrypted);
    free(encrypted);
    return 0;
}

这是加密,仅用于设置盐,然后使用“ crypt”使用盐对密码进行加密。

int main(int argc, char *argv[]) {
    cryptPassword("earlybird");
    crackRecursive(2);
    readDictionary("dictionary.txt");
}

最后,这是主要方法,我将密码设置为dictionary.txt文件中的“ earlybird”。我也尝试过使用“ aA”,但这没用。

方法打印出我想要的所有内容,裂缝打印出“ aa”,然后打印“ ab”,并继续遍历我提供的字母,直到最后。正确读取了字典。当然,只有当我告诉它在readDictionary中的“ crack”中的字符串和“ buffer”中对它进行printf时。

我怀疑我用盐弄错了,因为看起来哈希密码前面是盐,但是我的猜测没有。

0 个答案:

没有答案