低级文件I / O比较字符串

时间:2015-03-17 16:50:29

标签: c low-level-io

我正在编写一个程序,该程序接收带有3行加密密码的文件,并将它们与aaaa - zzzz中的所有4个小写字母进行比较。我遇到的唯一问题是我无法弄清楚如何逐行输入我输入的文件以及如何将它与单独的4个字母单词进行比较。然后它应该打印3个解密的4个字母单词,这些单词与文件中的密码相关。如果有任何关于如何改进我的代码的类型,请告诉我。我是初学者,所以如果可能的话我需要明确的解释和例子。谢谢。

EDIT *****

主要问题在于所有功能和主要功能。我不想将aaaa,aaab,aaac等打印到屏幕上,但是我想把它放在一个char数组中,以便我可以使用crypt将每个单独的文件与文件中的每一行进行比较。所以我需要建议如何将所有456976组合放入一个数组中,将其与每行代码进行比较,并将解决方案打印到屏幕上。

文件如下所示:

$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.
$1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0
$1$0lMKIuvE$7mOnlu6RZ/cUFRBidK7PK.

我的代码如下所示:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFFER_SIZE 1024

int my_fgets(char* buf, int len, int f)
{
   for (int i = 0; i < len; i++,buf++)
   {
      int count = read(f, buf, 1);
      if (!count || (buf[0] == '\n'))
      {
         buf[0] = 0;
         return i;
      }
   }
   return 0;
}

int inc(char *c,char begin, char end){
   if(c[0]==0) 
      return 0;
   if(c[0] == end){   // This make the algorithm to stop at char 'f'
      c[0]=begin;     // but you can put any other char            
      return inc(c+sizeof(char), begin, end);
   }   
   c[0]++;
   return 1;
}

char all(int a, int n,char begin, char end){
   int i, j;
   char *c = malloc((n+1)*sizeof(char));
   char result[] = "";
   for(i = a; i <= n; i++)
   {
      for(j=0;j<i;j++) c[j]=begin;
      c[i]=0;
      do {
         int k = sizeof(result);
         for (int g = 0; g < k -1; g++)
         {
            result[g] = c;
         }
      } while(inc(c,begin,end));
   }

   free(c);
}

int main(int argc, char* argv[])
{
   char *result;
   char let[456976];
   int f = open("pass.txt", O_RDONLY);
   if (f < 0) 
      return 0;
   char buf[1024];
   while (my_fgets(buf, sizeof(buf), f)) 
   {
      let = all(4,4,'a','z');
      int i = 0;
      result = crypt((let[i+1]), buf);
      int ok = strcmp (result, pass) == 0;
      return ok ? 0 : 1;

      all(4, 4, 'a', 'z');

   }
   close(f);
}

1 个答案:

答案 0 :(得分:0)

我认为你需要改写这个问题。也许下面的代码是你想要的。假设您有一个密码,并且您有一个包含加密密码列表(通常是哈希)的文件,您想要查看密码是对还是错。您将密码的哈希值与文件中的哈希值进行比较。我还没有测试过这段代码。

ps,让我知道如果我离开,我会删除答案。

void crypt(char* hash, const char* password_test) {
    //create hash from password, or encrypt it or something?
}

int test_password(const char *filename, const char *password){
    FILE *f;
    f = fopen(filename, "r");
    if (!f) return 0;
    char password_hash[256];
    crypt(password_hash, password);

    char hash[256];
    char buf[1024];
    while (fgets(buf, sizeof(buf), f) != NULL)
    {
        crypt(hash, buf);
        if (strcmp(password_hash, hash) == 0)
            return 1;
    }

    fclose(f);
    return 0;
}

void main() {
    int result = test_password("test.txt", "password");
    if (result == 1)
        printf("password is good\n");
}

使用open/read/close

逐行阅读
int my_fgets(char* buf, int len, int f)
{
    for (int i = 0; i < len; i++,buf++)
    {
        int count = read(f, buf, 1);
        if (!count || (buf[0] == '\n'))
        {
            buf[0] = 0;
            return i;
        }
    }
    return 0;
}

int main(){
    int f = open("test.txt", O_RDONLY);
    if (f < 0) return 0;
    char buf[1024];
    while (my_fgets(buf, sizeof(buf), f)) 
        printf("%s\n", buf);
    close(f);
}
相关问题