1
해시에서 최대 4 자의 암호를 해독하려고합니다. 이상적으로는 ./crack 50fkUxYHbnXGw
이라고 쓸 수 있으며 rofl
을 반환합니다.무차별 암호로 짧은 암호를 해독합니다. for 루프를 중첩했습니다.
내 접근 방식은 for 중첩 루프를 사용합니다. 이것을 다양한 길이의 문자열에 적용하려면 C에서 null 종결자를 사용하는 것이 좋지만 지금은 가능한 모든 길이에 대해 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 (소금, 해시, 2);'문제처럼 보입니다. – yano
네, 알파벳 ./-9A-Za-z의 두 글자의 소금입니다. 크립트의 결과는 두 글자에 이어 같은 알파벳의 글자가 11 개 더 나오므로 총 13 글자가됩니다. – Tarae