gpt4 book ai didi

比较 2 个文件中的子字符串

转载 作者:行者123 更新时间:2023-11-30 14:51:46 26 4
gpt4 key购买 nike

我有 2 个文件:shadow.txt 和 mytab2411.txt。 “shadow.txt”包含密码哈希值,而“mytab2411.txt”是包含密码及其相应哈希值的密码查找文件。我想要做的是使用“mytab2411.txt”通过比较“shadow.txt”和“mytab2411.txt”的哈希值来查找“shadow.txt”的密码。

我有两个问题:

  1. 我不知道如何从 mytab2411.txt 获取密码文本。

  2. 我得到这个输出而不是所需的输出:

用户 ID:pyc 1 -密码(未找到)

用户 ID:pyc 2 -密码(未找到)

用户 ID:pyc 3 -密码(未找到)

用户 ID:pyc 4 -密码(未找到)

用户 ID:pyc 5 -密码(未找到)

用户 ID:pyc 6 -密码(未找到)

shadow.txt

pyc1:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::
pyc2:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1:17482:0:99999:7:::
pyc3:$1$$zZQKNjRd94GHyYOwXuStf0:17482:0:99999:7:::
pyc4:$6$iKYSRG68$STdY8TCgoCaNfSUcyCwSBlVekdjs0P3qXtwxSbgpQpMUnHJRRSHOT5amoR24IqZBTPNWuIfO.uhZEnGLuE4q/.:17482:0:99999:7:::
pyc5:$6$$FDqvMBbQOCyKP9uBL8E6TAEupCh72v.3/ow4fZ5HpZ/0NS7LBifFS9nJdzc/u2OEhUnRF9yC4Lw23hHjD1EmD.:17482:0:99999:7:::
pyc6:$6$$LfCuhKecDtIfX77LOTWD1PjhF1IC0hBzjxckEthmoT8mVbxKH3qJzFgEi/P9GN1mptR4WPiwuh69X/41M6pHW1:17482:0:99999:7:::

mytab2411.txt(缩短版本,因为有超过 20 万行)

apple:$1$$Tnq7a6/C1wwyKyt0V/.BP/
apple:$6$$vTqYXuMRNbK5N1xiTvUKcJuKVmEQyPtgUiyawaEBMwknJ3AQoOvPpr2RrANRxDTS.qo7rQuFvxZcUkT31W6uG/
banana:$1$$zZQKNjRd94GHyYOwXuStf0
banana:$6$$5iQBiKBv7vIGqC5iQJOVUpzgnSO0P.pMQ.Guwczcn9nQSu61IVKT9GU4IEYjb5WbsBLaIfZ3io59M4oac.W0/1
orange:$1$$Ro.kDk5GNLNQbdJyDEovy1
orange:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1

所需输出

user id : pyc1 – password found => apple

user id : pyc2 – password found => orange

user id : pyc3 – password found => banana

Data Error: Invalid entry found in the shadow file. (skipped)

user id : pyc5 – password (NOT FOUND)

user id : pyc6 – password (NOT FOUND)

主要代码

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
int printDataError(){

printf("Data error: Invalid entry found in the shadow file. (Skipped)\n");
}

int main(void)
{
{
char buf[1024];
char **arr1 = NULL;
char **arr2 = NULL;
int size1 = 0;
int size2 = 0;
FILE * f1, *f2;
f1 = fopen("shadow.txt", "r");
f2 = fopen("mytab2411.txt", "r");

// Allocate memory for shadow.txt
while(fgets(buf, 1024, f1))
{
size1++;
arr1 = realloc(arr1, sizeof(char*) * size1);
arr1[size1 - 1] = strdup(buf);
}

// Allocate memory for mytab2411.txt
while(fgets(buf, 1024, f2))
{
size2++;
arr2 = realloc(arr2, sizeof(char*) * size2);
arr2[size2 - 1] = strdup(buf);
}


char line[1000]; //Allocate max number of characters in a line for shadow.txt
char line2[1000]; //Allocate max number of characters in a line for mytab2411.txt

char hash[1000]; //Allocate max number of characters in a hash (substring of a line) for shadow.txt
char hash2[1000]; //Allocate max number of characters in a hash (substring of a line) for shadow.txt

char md5[5]= "$1$$"; // Define string to be searched for md5
char sha512[5]="$6$$"; //Define string to be searched for sha512
char *ret; // Used for shadow.txt
char *ret2; //Used for shadow.txt
char * ret3; //Used for mytab2411.txt
char * ret4; //Used for mytab2411.txt

// Read shadow.txt line by line
for(int i = 0; i < size1; i++) {

memset(hash, '\0', sizeof(hash));
strcpy(line, arr1[i]);

//Search for md5 in shadow.txt
md5[4]='\0';
ret = strstr(line, md5);

//Search for sha512 in shadow.txt
sha512[4]='\0';
ret2 = strstr(line, sha512);

// Copies md5 hash to the variable hash if md5 is detected in shadow.txt
if (ret){
strncpy(hash, line+5, 26);
hash[26] = '\0';
//printf("pyc %d hash: %s\n", i+1,hash);

}

// Copies sha512 hash to the variable hash if sha512 is detected in shadow.txt
else if (ret2){

strncpy(hash, line+5, 90);
hash[90] = '\0';
//printf("pyc %d hash: %s\n", i+1,hash);


}

// Read mytab2411.txt line by line
for(int j = 0; j < size2; j++){

memset(hash2, '\0', sizeof(hash2));
strcpy(line2, arr2[j]);

//Search for md5 in mytab2411.txt
md5[4]='\0';
ret3 = strstr(line2, md5);

//Search for sha512 in mytab2411.txt
sha512[4]='\0';
ret4 = strstr(line2, sha512);

// Copies sha512 hash to the variable hash if md5 is detected in mytab2411.txt
if (ret3){

strcpy(hash2, &line2[strlen(line2) - 27]);

//printf("Line %d hash: %s\n", j+1,hash2);

}

// Copies sha512 hash to the variable hash if sha512 is detected in mytab2411.txt
else if (ret4){

strcpy(hash2, &line2[strlen(line2) - 91]);

//printf("Line %d hash: %s\n", j+1,hash2);

}

}//End of "for(int j = 0; j < size2; j++)" loop

// Compares the hash in shadow.txt (hash) and hash in mytab2411.txt (hash2).
if(strcmp(hash,hash2) == 0)
printf("user id: pyc %d - password found => \n",i+1);

else if (strcmp(hash,hash2) != 0)
printf("user id: pyc %d -password <NOT FOUND>\n",i+1);


else
printDataError();

}//End of "for (int i = 0; i < size1; i++)" loop


return 0;
} //End of main

最佳答案

两个文件中的密码已经进行了哈希处理。您不应该自己计算任何哈希值。相反,您应该将密码读取为文本,然后简单地进行比较。

关于比较 2 个文件中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48033270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com