Well, here's a very simple program to write the entries into a file, using the unix/linux/bsd crypt() function to encrypt the password and getpass() function to get the password unix style (no keyboard echo, at all):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(int argc, char **argv){
char salt[2]; /* Salt for the crypt() function */
const char *salt_chars = "abcdefghijklmnopqrstuvwxyz" /* Range of character supported */
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* as a value for salt in crypt() */
"0123456789";
char username[BUFSIZ], password1[BUFSIZ], /* Buffers for user input and comparison */
password2[BUFSIZ], *buf;
char filename[BUFSIZ]; /* Buffer for filename */
FILE *outfile; /* File handle */
/* Build salt */
srand(time(NULL));
salt[0] = salt_chars[rand() % 62];
salt[1] = salt_chars[rand() % 62];
/* Get the filename */
printf("Enter Password Filename: ");
scanf("%s", filename);
/* Get the username */
printf("Username: ");
scanf("%s", username);
do {
/* Get the password */
buf = getpass("Password: ");
/* Copy to a "stable" pointer */
sprintf(password1, "%s", buf);
/* Get the password */
buf = getpass("Enter Again: ");
/* Copy to a "stable" pointer */
sprintf(password2, "%s", buf);
/* See if the passwords are the same */
if(strcmp(password1, password2) != 0)
printf("\nPasswords do not match!\nTry again.\n\n");
} while(strcmp(password1, password2) != 0);
/* Encrypt the password */
buf = crypt(password1, salt);
/* Open the output file for append */
if((outfile = fopen(filename, "a+")) == NULL){
printf("\nFile error!\nAborting...\n");
} else {
/* Print the record to the file */
fprintf(outfile, "%s:%s\n", username, buf);
} /* End if */
/* Close the file */
fclose(outfile);
} /* End main() */
And here's how you would check it:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv){
char username[BUFSIZ], *password; /* User input buffers */
char buf[BUFSIZ]; /* File input buffer */
char *user_file, *pass_file; /* Buffers for values in file */
char filename[BUFSIZ]; /* Filename buffer */
FILE *infile; /* File handle */
/* Get the filename */
printf("Enter Password Filename: ");
scanf("%s", filename);
/* Get the username from the user */
printf("Username: ");
scanf("%s", username);
/* Get the password from the user */
password = getpass("Password: ");
/* Open the file */
if((infile = fopen(filename, "r")) == NULL){
printf("\nFile error!\nAborting...\n");
} else {
/* Loop throught the file */
while (!feof(infile)) {
/* Initialize with empty string */
buf[0] = '\0';
/* Read in one line */
fscanf(infile, "%s", buf);
/* If it's an empty line, continue */
if(strlen(buf) == 0) continue;
/* Point to the buffer */
user_file = buf;
/* Point to the delimiter in the buffer */
pass_file = strchr(buf, ':');
/* Change the delimiter to a nul character */
pass_file[0] = '\0';
/* Move to the next character */
pass_file++;
/* See if this matches the name the user entered */
if(strcmp(user_file, username) == 0){
/* See if the passwords match */
if(strcmp(crypt(password, pass_file), pass_file) == 0){
printf("Correct password...\n");
} else {
printf("Invalid password!\n\n");
} /* End if */
/* We found what we wanted; get out of loop */
break;
} /* End if */
} /* End while */
} /* End if */
/* Close the file */
fclose(infile);
} /* End main() */
That's basically all there is to it. I used the most basic form--there are better ways (using a variety of encryption algorithms--like ones using stronger encryption than 56 bit, like this one does). Keep in mind, you have to compile in the crypt shared library--like this using gcc:
gcc -lcrypt -o program_name program_name.c
Otherwise, it'll blow up when you run it (coredump).
Bookmarks