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.
- How to decrypt the file line by line and save it to a char []
- 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.