我正在称为CS50的EDX当然执行。也许你们有些人很熟悉它。Ç - 隐窝() - 代码需要更长的时间,用5环或多于4个环,但使用相同的参数/散列
一个设置问题,请您实现一个算法破解这是使用DES为基础的加密散列和高达4个字符的密码。
到目前为止,这么好。我已经完成了。
但我还是决定提高一点点,并有可能破解是最多8个字符,这是基于DES加密的密码最大。
而问题是,当我添加了第五个字符(或更多)的可能性,我的代码不工作了。
这里是我的代码
这一个工作:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
/*
Use this to compile
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow crack4.c -lcrypt -lm -o crack4
*/
int main(int argc, char *argv[])
{
if (argc != 2) //Checks if number of command-line arguments is valid
{
printf ("usage: ./crack + hash \n");
return 1; //Retuns 1 (error)
}
char *hash = argv[1]; //Gets hash passed as argument
char salt[3]; //Gets the salt
salt[0] = hash[0];
salt[1] = hash[1];
salt[2] = '\0';
//All possible characters used in a DES-based hashed password (taken from gnu library)
const char *const seedchars = " ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char text[9] = "\0"; //Text that is gonna be tried with crypt()
for (int d = 0; d <= 64 ; d++) //To check for passwords of up to 4 characters
{
printf("d %d \n", d);
if(d > 0)
{
text[4] = '\0'; //Defining null-terminator at index 4
text[3] = seedchars[d]; //Iterates through the seedchars list at index 3
}
for (int c = 0; c <= 64 ; c++) //To check for passwords of up to 3 characters
{
if(c > 0)
{
if (d == 0)
{
text[3] = '\0'; //Defining null-terminator at index 3
}
text[2] = seedchars[c]; //Iterates through the seedchars list at index 2
}
for (int b = 0; b <= 64 ; b++) //To check for passwords of up to 2 characters
{
if(b > 0)
{
if (c == 0 && d == 0)
{
text[2] = '\0'; //Defining null-terminator at index 2
}
text[1] = seedchars[b]; //Iterates through the seedchars list at index 1
}
for (int a = 0; a <= 64 ; a++) //To check for passwords of up to 1 character
{
if(b == 0 && c == 0 && d == 0)
{
text[1] = '\0'; //Defining null-terminator at index 1
}
text[0] = seedchars[a]; //Iterates through the seedchars list at index 0
char *password = crypt(text, salt); //Hash var text and save it to var password
if (strcmp(hash, password) == 0) //Compares the hash passed as argv with created above
{
printf("%s\n", text); //prints the text that led to said hash
return 0; //Returns 0 (okay)
}
}
}
}
}
return 1; //Retuns 1 (error)
}
这一个不工作:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
/*
Use this to compile
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow crack.c -lcrypt -lm -o crack
*/
int main(int argc, char *argv[])
{
if (argc != 2) //Checks if number of command-line arguments is valid
{
printf ("usage: ./crack + hash \n");
return 1; //Retuns 1 (error)
}
char *hash = argv[1]; //Gets hash passed as argument
char salt[3]; //Gets the salt
salt[0] = hash[0];
salt[1] = hash[1];
salt[2] = '\0';
//All possible characters used in a DES-based hashed password (taken from gnu library)
const char *const seedchars = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char text[9] = "\0"; //Text that is gonna be tried with crypt()
for (int h = 0; h <= 64 ; h++) //To check for passwords of up to 8 characters
{
if(h > 0)
{
text[8] = '\0'; //Defining null-terminator at index 8
text[7] = seedchars[h]; //Iterates through the seedchars list at index 7
}
for (int g = 0; g <= 64 ; g++) //To check for passwords of up to 7 characters
{
if(g > 0)
{
if (h == 0)
{
text[7] = '\0'; //Defining null-terminator at index 7
}
text[6] = seedchars[g]; //Iterates through the seedchars list at index 6
}
for (int f = 0; f <= 64 ; f++) //To check for passwords of up to 6 characters
{
if(f > 0)
{
if (g == 0 && h == 0)
{
text[6] = '\0'; //Defining null-terminator at index 6
}
text[5] = seedchars[f]; //Iterates through the seedchars list at index 5
}
for (int e = 0; e <= 64 ; e++) //To check for passwords of up to 5 characters
{
if(e > 0)
{
if (f == 0 && g == 0 && h == 0)
{
text[5] = '\0'; //Defining null-terminator at index 5
}
text[4] = seedchars[e]; //Iterates through the seedchars list at index 4
for (int d = 0; d <= 64 ; d++) //To check for passwords of up to 4 characters
{
printf("d %d \n", d);
if(d > 0)
{
if (e == 0 && f == 0 && g == 0 && h == 0)
{
text[4] = '\0'; //Defining null-terminator at index 4
}
text[3] = seedchars[d]; //Iterates through the seedchars list at index 3
}
for (int c = 0; c <= 64 ; c++) //To check for passwords of up to 3 characters
{
if(c > 0)
{
if (d == 0 && e == 0 && f == 0 && g == 0 && h == 0)
{
text[3] = '\0'; //Defining null-terminator at index 3
}
text[2] = seedchars[c]; //Iterates through the seedchars list at index 2
}
for (int b = 0; b <= 64 ; b++) //To check for passwords of up to 2 characters
{
if(b > 0)
{
if (c == 0 && d == 0 && e == 0 && f == 0 && g == 0 && h == 0)
{
text[2] = '\0'; //Defining null-terminator at index 2
}
text[1] = seedchars[b]; //Iterates through the seedchars list at index 1
}
for (int a = 0; a <= 64 ; a++) //To check for passwords of up to 1 character
{
if(b == 0 && c == 0 && d == 0 && e == 0 && f == 0 && g == 0 && h == 0)
{
text[1] = '\0'; //Defining null-terminator at index 1
}
text[0] = seedchars[a]; //Iterates through the seedchars list at index 0
char *password = crypt(text, salt); //Hash var text and save it to var password
if (strcmp(hash, password) == 0) //Compares the hash passed as argv with created above
{
printf("%s\n", text); //prints the text that led to said hash
return 0; //Returns 0 (okay)
}
}
}
}
}
}
}
}
}
}
return 1; //Retuns 1 (error)
}
我在这两个代码使用下面的哈希值,但它不适用于第二个代码。
hash - 50fkUxYHbnXGw
text - rofl
有人可以帮我理解为什么它不工作?
谢谢。
编辑:
第二个代码刚刚结束运行,它实际上是工作,但它采取的方式更长的时间来破解密码。下面是截图:
我不能够将它张贴作为某些原因图像,所以这里的链接http://imgur.com/a/GVWar
编辑2:添加链接的图像和固定标题
编辑3:重新修复标题
对不起,但“不工作”不是一个有用的问题描述。它也不适合像SO这样的问答网站。 – StoryTeller
“它不起作用。为什么它不工作”....“它实际上是在工作”......可能应该编辑你的问题,只是说出问题是什么,而不是混淆。 – kaylum
修复你的标题:你的代码有效,所以你真正的问题是为什么它需要这么长时间。法比奥回答如下。 – TheGreatContini