1
我試圖破解的最多4個字母的密碼,從一個哈希值。理想情況下,我可以寫./crack 50fkUxYHbnXGw
並返回rofl
。破解用蠻力短密碼:嵌套的for循環
我的做法是嵌套的for循環。爲了將其應用於不同長度的字符串,我被推薦在C中使用一個空終止符,但現在我已經嘗試了每一個可能長度的if語句。
你知不知道這樣做的更優雅的方式?
最後,我有沒有給我任何輸出的錯誤。
#define _XOPEN_SOURCE // for crypt
#include <cs50.h> // used for get_string
#include <stdio.h> // used for printf
#include <string.h> // used for strlen
#include <ctype.h> // used for isalpha
#include <stdlib.h> // for atoi
#include <crypt.h> // for crypt
int main(int argc, char* argv[])
{
// accept only two arguments from command line
// hash is max 13 characters. First two are the salt.
// next 11 are the key
if (argc != 2)
{
printf("Usage: ./crack hash \n");
return 1;
}
// make second command line argument into string
char* hash = argv[1];
char* salt = argv[1];
strncpy(salt, hash, 2);
salt[2] = 0; // null terminates destination
char* key = hash + 2; // just key without salt
char* abAB = "abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM";
for (int i = 0, n = strlen(abAB); i < n; i++)
{ //check for every length
char tri1[1] = {abAB[i]};
// produce my own encrpyted key with salt
char* cr1 = crypt(tri1, salt);
// if own key is equal to key provided, print out the string tried.
int comp1 = strcmp(cr1, key);
if (comp1 == 0)
{
printf("%s", tri1);
}
for (int j = 0, m = strlen(abAB); j < m; j++)
{
char tri2[2] = {abAB[i],abAB[j]};
char* cr2 = crypt(tri2, salt);
int comp2 = strcmp(cr2, key);
if (comp2 == 0)
{
printf("%s", tri2);
}
for (int k = 0, p = strlen(abAB); k < p; k++)
{
char tri3[3] = {abAB[i],abAB[j],abAB[k]};
char* cr3 = crypt(tri3, salt);
int comp3 = strcmp(cr3, key);
if (comp3 == 0)
{
printf("%s", tri3);
}
for (int l = 0, q = strlen(abAB); l < q; l++)
{
char tri4[4] = {abAB[i],abAB[j],abAB[k],abAB[l]};
// produce my own encrpyted key with salt
char* cr4 = crypt(tri4, salt);
// if own key is equal to key provided, print out the string tried.
int comp4 = strcmp(cr4, key);
if (comp4 == 0)
{
printf("%s", tri4);
}
}
}
}
}
printf("\n");
return 0;
}
你正在尋找一個DFS。 – SLaks
'char * hash = argv [1]; char * salt = argv [1]; strncpy(salt,hash,2);'看起來很麻煩。 – yano
是這是正確的,從字母表./0-9A-Za-z兩個字符鹽,和隱窩的結果將是那些兩個字符,接着從同一個字母11以上,在總讓13。 – Tarae