匹配2个文件中的文本

问题描述 投票:-2回答:1

我编写了一个程序,旨在通过搜索两个文本文件中存在的匹配哈希来恢复Linux系统密码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXCHAR 1000
//Declaring Functions to match word in file
int matchfile(char *shadowfilename, char*hashtablefilename);
//shadowfilename for shadow.txt hashtablefilename for hash table
void UsageInfo(char *shadowfile, char * hashtablefile );
//Display usage info on arguements for program
void UsageInfo(char *shadowfile, char * hashtablefile) {
printf("Usage: %s %s <shadowfile> <hashtable>\n", shadowfile,hashtablefile);

}

//main function.
int main(int argc, char *argv[]) {
    int result, errcode;
    //Display format for user to enter arguements and
    //End program if user does not enter exactly 3 arguements
    if(argc < 3 || argc > 3) {
        UsageInfo(argv[1],argv[2]);
        exit(1);
    }


    system("cls");
//Pass command line arguements into searchstringinfile
    result = matchfile(argv[1], argv[2]);

//Display error message
    if(result == -1) {
        perror("Error");
        printf("Error number = %d\n", errcode);
        exit(1);
    }
    return(0);
}
//Declaring Functions to match word in file
//int matchfile(char *shadowfilename, char *hashtablefilename);
//shadowfilename for shadow.txt hashtablefilename for hash table

int matchfile(char *shadowfilename, char *hashtablefilename){

    FILE *shadowfile;
    FILE *hashtable;
    char strshadow[MAXCHAR];
    char strhash[MAXCHAR];


    shadowfile = fopen(shadowfilename, "r");
    if (shadowfile == NULL){
        printf("Could not open file %s",shadowfilename);
        return 1;
    }


    hashtable = fopen(hashtablefilename, "r");
    if (hashtable == NULL){
        printf("Could not open file %s",hashtablefilename);
        return 1;
    }

    //Getting text from the 2 files
    while (fgets(strshadow, MAXCHAR, shadowfile) != NULL &&fgets(strhash,MAXCHAR, 
    hashtable) != NULL){
    printf("%s", strshadow);
    printf("%s", strhash);
    int linenumber = 1;
    int search_result = 0;
            //Matching words line-by-line

    if((strstr(strshadow,strhash)) != NULL) {
        //Display line in which matched word is found
        printf("A match found on line: %d\n", linenumber);
        printf("\n%s\n", strhash);
        search_result++;
    }
    linenumber++;
}


fclose(shadowfile);
return 0;

}

但是,由于前面的字符,我无法匹配两个文件中存在的两个哈希值。

hashtable.txt。此文件包含纯文本中缺少的密码,并且是相应的哈希值。格式如下:(密码):(哈希)

banana:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::

shadow.txt。此文件包含纯文本帐户用户名,并且是相应的哈希值。格式如下:(用户):(哈希)

pyc1:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::

如上所示,单词'banana'和'pyc1'阻止程序检测到两个哈希值被检测到。有人能告诉我为克服这个需要做出的改变吗?谢谢。编辑:说明shadow.txt和hashtable.txt的格式

c
1个回答
0
投票

在满足某些条件之前跳过字符串中字符的最简单方法是:

char someString[MAXCHAR];

for (char* ptr = someString; *ptr != '\0'; ptr++) {
    if (conditionIsMet(ptr)) {
        doSomething();
        break;
    }
}

在你的情况下,conditionIsMet(ptr)应该将*ptr':'进行比较,在这种情况下,密码哈希值在(ptr + 1)下(字符串从下一个字符开始)。我想你可以自己编写其余的代码。

© www.soinside.com 2019 - 2024. All rights reserved.