c - 匹配 2 个文件中的文本

标签 c

我编写了一个程序,旨在通过搜索两个文本文件中存在的匹配哈希值来恢复 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 arguments 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 arguments and
    //End program if user does not enter exactly 3 arguments
    if(argc < 3 || argc > 3) {
        UsageInfo(argv[1],argv[2]);
        exit(1);
    }


    system("cls");
//Pass command line arguments 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;

}

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

哈希表.txt。 该文件包含丢失的纯文本密码以及相应的哈希值。 格式如下:(密码):(哈希值)

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

影子.txt。该文件包含纯文本形式的帐户用户名以及相应的哈希值。 格式如下:(用户):(hash)

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

如上所示,单词“banana”和“pyc1”阻止程序检测这两个哈希值。 有人可以告诉我需要做出哪些改变来克服这个问题吗? 谢谢。 编辑:澄清shadow.txt和hashtable.txt的格式

最佳答案

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

char someString[MAXCHAR];

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

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

关于c - 匹配 2 个文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48007768/

相关文章:

c++ - 我的窗口句柄未使用,无法评估

c - 如何在 C 中仅提取此字符串的一部分?

c - 线程互斥 : mutex_lock implemented with mutex_trylock

c++ - 我需要帮助来理解 CLOCKS_PER_SEC

c++ - 从不同线程调用 recvfrom() 和 setsockopt() 添加/离开多播成员

c++ - 连续内存块中的位计数

c - 对与c中的字符串数组相关的数组进行排序

c++ - libSDL2main.a 的用途是什么?

c - Valgrind 内存泄漏可达

c - C 中的二叉树