title: Low Level IO with Crypt
body: <p>I am trying to compare a encrypted string that is taken from each line of a file to AAAA-ZZZZ until it finds its match of the password. I am guaranteed that the user password is of 4 characters. What I am trying to do is take in the file using LowLevel IO and output to a new file with the decrypted passwords of each line. I am not the best at C programming yet so please be gentle. I need direction on how to create an array or list going from AAAA all the way to ZZZZ and then comparing each to the decrypted version of the file line. </p>
  
  <ol>
  <li>How to decrypt the file line by line and save it to a char []</li>
  <li>How to compare each line to another char [] until password is found</li>
  </ol>
  
  <p>For Example:</p>
  
  <p>if the line is $1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B. and the next line is $1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0. Assuming the resulting password after decryption is ABSZ and TAZE the new file will result it ABSZ on the first line and TAZE for the second line. </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  void pdie(const char *);
  void die(const char *);
  
  #define BUFFER_SIZE 1024 
  
  int main(void)
  {
     char *pass;
     int rfd;   
     int wfd;   
     char buffer[BUFFER_SIZE];   
     char *bp;   
     int bufferChars;   
     int writtenChars;  
  
     if ((rfd = open("pass.txt", O_RDONLY, 0)) &lt; 0)
        pdie("Open failed");
  
     if ((wfd = open("passout.txt", O_WRONLY | O_CREAT | O_TRUNC,
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) &lt; 0)
        pdie("Open failed");
  
     while (1)
     {   
        if ((bufferChars = read(rfd, buffer, BUFFER_SIZE)) &gt; 0)
        {
           printf("%s", buffer);
  
           bp = buffer; 
           pass = crypt(getpass(all(4,4,'a','z')), *bp);  
  
           printf(pass);
           while (bufferChars &gt; 0)
           {
              if ((writtenChars = write(wfd, bp, bufferChars)) &lt; 0)
                 pdie("Write failed");
  
              bufferChars -= writtenChars;
              bp += writtenChars;
           }
        }
        else if (bufferChars == 0)  
           break;
        else   
           pdie("Read failed");
     }
     close(rfd);
     close(wfd);
  
     return 0;
  }
  
  
  void pdie(const char *mesg) {
  
     perror(mesg);
     exit(1);
  }
  
  void die(const char *mesg) {
  
     fputs(mesg, stderr);
     fputc('\n', stderr);
     exit(1);
  }
  
  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;
  }
  
  int all(int a, int n,char begin, char end){
      int i,j;
      char *c = malloc((n+1)*sizeof(char));
      for(i=a;i&lt;=n;i++){
          for(j=0;j&lt;i;j++) c[j]=begin;
          c[i]=0;
          do {
              printf("%s\n",c);
          } while(inc(c,begin,end));
      }
      free(c);
  }
  </code></pre>
  
  <p>here is the file:</p>
  
  <pre><code>$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.
  $1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0
  $1$0lMKIuvE$7mOnlu6RZ/cUFRBidK7PK.
  </code></pre>