1

I'm writing a program that takes a file with the 3 lines of encrypted passwords and compares them to all 4 lower case letters from aaaa - zzzz. The only issue I am having is that I can't figure out how to go line by line of the file I input and how to compare it to the 4 letter words individually. It should then print the 3 decrypted 4 letter words that correlate to the passwords from the file. Also if there are any types on how to improve my code, please let me know. I'm a beginner at this so I need clear explanations and examples if possible. Thank you.

EDIT*****

The main problem is in the all function and main. I'm not wanting to print the aaaa, aaab, aaac, etc to the screen but I want to put it in an char array so that i can compare each individually to each line from the file using crypt. So I need advice on how to put all 456976 combinations into an array, compare it to each line of code, and print the solutions to the screen.

file looks like this:

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

My code looks like this:

#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);
}
kids
  • 57
  • 7
  • Why `ssize_t r = read(rfd, buffer, 1); return r;`? Seems odd to return the number of things read. I'd expect `ssize_t r = read(rfd, buffer, 1); return buffer[0];` with some error checking. – chux - Reinstate Monica Mar 17 '15 at 18:01
  • Oh i could see that. Im also having trouble converting the line of code to a char* characters so I can use it to be tested in the crypt function.... @chux – kids Mar 17 '15 at 18:10
  • Edited the question for new problems – kids Mar 17 '15 at 22:17
  • Serious problems. `result` does not exist, it's only declared as a pointer. `crypt` should not return pointer like that. `all` is not doing anything. You are just typing in words and you ignore compiler's errors and warnings. You have to start with a basic tutorial, break the problem in to smaller parts instead of solving a larger puzzle. – Barmak Shemirani Mar 17 '15 at 23:19

1 Answers1

0

I think you need to reword the question. Maybe the code below is what you want. Let's say you have a password, and you have a file which contains a list of encrypted passwords (or hash usually), you want to see if password is right or wrong. You compare the hash of the password with hashes in the file. I haven't tested this code.

ps, let me know if I am way off, I'll delete the answer.

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");
}

Reading line by line using 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);
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • That's close but I am mainly looking for how to read my file line by line using low level I/O and how it would compare it to aaaa, aaab, etc. individually from my all function. Does that make sense? – kids Mar 17 '15 at 17:58
  • Note: no need for 256-1, `fgets(buf, sizeof buf, f)` is fine. Remember to cope with the `'\n'` in the `strcmp()`. – chux - Reinstate Monica Mar 17 '15 at 18:12
  • Reading file line by line: http://stackoverflow.com/a/3501681/4603670 @chux, thanks, fixed it – Barmak Shemirani Mar 17 '15 at 18:20
  • I need to use low level I/O though. That doesn't use low level i/o calls. – kids Mar 17 '15 at 18:23
  • C is all low level. I am not sure what that means, I added another function `my_fgets` – Barmak Shemirani Mar 17 '15 at 19:17
  • Edited the question for new problems – kids Mar 17 '15 at 22:17
  • By the way, I still don't know what you mean by low-level I/O. If you are just starting, you might want to start with a high-level language like Python or Java or something. High-level means easy, low-level means hard. C is sort of mid-level and it is a disaster as a beginner's language. Forget about that function `my_fgets` which I made up. Standard functions are better. – Barmak Shemirani Mar 18 '15 at 00:21