Я работаю над Pset 2 crack.c и до сих пор, и мне удалось получить общую концепцию.Тем не менее, мой код по-прежнему не работает.Он компилируется и запускается, но ничего не печатает.
Я не совсем уверен, что здесь не так, возможно, я что-то упускаю?
#include <stdio.h>
#include <cs50.h>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <crypt.h>
#include <string.h>
char plaintext[4];
char salt [3];
string alphapool;
string newhash;
int main (int argc, string argv[]) {
if (argc != 2)
{
printf("Error: Input 2 command line arguments\n");
return 1;
}
else
{
alphapool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
salt[0] = argv[1][0]; salt[1] = argv[1][1]; salt[2] = '\0';
//match a plaintext guess to an array in possibilities (alpha pool)
//for one letter
for(int i = 0; i < 26+26; i++)
{ //single letter
plaintext[0] = alphapool[i];
plaintext[1] = '\0';
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s",plaintext);
return true;
}
}
for(int i = 0; i < 26+26; i++)
{//double letter
plaintext[0] = alphapool[i];
for(int j = 0; j < 26+26; j++)
{
plaintext[1] = alphapool[i];
plaintext[2] = '\0';
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s", plaintext);
return true;
}
}
}
for(int i = 0; i < 26+26; i++)
{//three letter
plaintext[0] = alphapool[i];
for(int j = 0; j < 26+26; j++)
{
plaintext[1] = alphapool[i];
for (int k = 0; k < 26+26; k++)
{
plaintext[2] = alphapool[i];
plaintext[3] = '\0';
}
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s", plaintext);
return true;
}
}
}
for(int i =0; i < 26+26; i++)
{
//four letter word
plaintext[0] = alphapool[i];
for(int j = 0; j < 26+26; j++)
{
plaintext[1] = alphapool[i];
for (int k = 0; k < 26+26; k++)
{
plaintext[2] = alphapool[i];
for (int l = 0; l < 26+26; l++)
{
plaintext[3] = alphapool[i];
}
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s", plaintext);
return true;
}
}
}
}
return 0;
}
}
Я ожидаю, что сгенерированный открытый текст должен быть значением, которое произвело хэш, который соответствует данномухэш.