1

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.

  1. How to decrypt the file line by line and save it to a char []
  2. How to compare each line to another char [] until password is found

For Example:

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.

This is what I have so far:

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

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)) < 0)
      pdie("Open failed");

   if ((wfd = open("passout.txt", O_WRONLY | O_CREAT | O_TRUNC,
                   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
      pdie("Open failed");

   while (1)
   {   
      if ((bufferChars = read(rfd, buffer, BUFFER_SIZE)) > 0)
      {
         printf("%s", buffer);

         bp = buffer; 
         pass = crypt(getpass(all(4,4,'a','z')), *bp);  

         printf(pass);
         while (bufferChars > 0)
         {
            if ((writtenChars = write(wfd, bp, bufferChars)) < 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<=n;i++){
        for(j=0;j<i;j++) c[j]=begin;
        c[i]=0;
        do {
            printf("%s\n",c);
        } while(inc(c,begin,end));
    }
    free(c);
}

here is the file:

$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.
$1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0
$1$0lMKIuvE$7mOnlu6RZ/cUFRBidK7PK.
kids love
  • 61
  • 2
  • ??? Can anyone help? – kids love Jun 02 '15 at 23:25
  • I've looked at this a couple of times, and I can't make head or tail of what you're doing. Is the `crypt()` function the one that's found on Unix-like machines, or something of your own devising? You need a plain text, a key (password), and the encrypted text. It isn't clear to me what you're planning to use as the plain text or the key. If the `crypt()` function is the normal Unix-like one, then you won't be able to decrypt anything. You will be able to encrypt something with the key and see if it matches the encrypted text, but that's about all. Maybe you need to explain your problem again. – Jonathan Leffler Jun 03 '15 at 03:13
  • @JonathanLeffler posted new one here with better understanding http://stackoverflow.com/questions/30609950/very-simple-crypt-program-error/30610072?noredirect=1#comment49287501_30610072 – kids love Jun 03 '15 at 03:46
  • I'd like to recommend removing this question, hard though it may be to stomach losing the rep from the up-vote. I'll take another look at the other question. – Jonathan Leffler Jun 03 '15 at 06:04

0 Answers0