Project-3 / C cypher / encrypt.c
encrypt.c
Raw
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int encryption();
int decryption();
int map_encryption_error();
int map_decryption_error();

int main(int argc, char *argv[]) {

    

    char *mappingfile = NULL;
    char *inputfile = NULL;
    int encryption_mode = 0;
  


    for (int i = 1; i < argc; i++) {

        if (strcmp(argv[i], "-t") == 0) {
            mappingfile = argv[++i];
        }

        else if (strcmp(argv[i], "-i") == 0) {
            inputfile = argv[++i];
        }

        else if (strcmp(argv[i], "-m") == 0) {
            encryption_mode = atoi(argv[++i]);
        }
    }

    // if the input mappingfile is not found:

    if (encryption_mode == 1) {
        if (strcmp(mappingfile, "mapping_encryption.csv") != 0) {
            fprintf(stderr, "Error: Mapping file mapping_encryption.csv does not exist\n");
            return 3;
        }
        
    }

    if (encryption_mode == 2) {
        if (strcmp(mappingfile, "mapping_decryption.csv") != 0) {
            fprintf(stderr, "Error: Mapping file mapping_decryption.csv does not exist\n");
            return 3;
        }   
    }

    // errors realted to mapping files
    
     if (encryption_mode == 1) {
        if (map_encryption_error()) {
            return 4;
        }
     }

    if (encryption_mode == 2) {
        if (map_decryption_error()) {
            return 4;
        }
    }   
    
    
    
    // input file inputfile is not found:

    if (encryption_mode == 1) {
        if (strcmp(inputfile, "input_encryption.txt") != 0) {
            fprintf(stderr, "Error: Input word file input_encryption.txt does not exist\n" );
            return 5;
        } 
    }

    if (encryption_mode == 2) {
        if (strcmp(inputfile, "input_decryption.txt") != 0) {
            fprintf(stderr, "Error: Input word file input_decryption.txt does not exist\n");
            return 5;
        }  
    }

    //others

    if (argc != 7) { 
        fprintf(stderr, "Usage: ./encrypt -t <mappingfile> -m <encryption mode> -i <inputfile>\n" );
        return 7;
    }

    // if the user passes in an incorrect mode:
    if (encryption_mode != 1 && encryption_mode != 2) {
        fprintf(stderr, "You entered %d. Sorry, your mode must be 1 for encryption or 2 for decryption\n", encryption_mode);
        return 6;
    }


    if (encryption_mode == 1) {
        encryption();
        return 0;
    }
    
    if (encryption_mode == 2) {
        decryption();
        return 0;
    }
    
}

int encryption() {

    char input_word[100];
    char letters[26]; 
    char encrypted_letters[26]; 
    char output_word[100];
    int len;
    char letter;
    int index;
    char temp;

    // stores the letters and the encrypted letters in 2 seperate arrays
    FILE *fp;
    fp = fopen("mapping_encryption.csv", "r");
    for (int i = 0; i < 26; i++) {
        fscanf(fp, "%c,%c\n", &letters[i], &encrypted_letters[i]);
    }
    fclose(fp);

    fp = fopen("input_encryption.txt", "r");
    FILE *fp1;
    fp1 = fopen("output_encryption.txt", "w");

    // replacing the letters with the encrypted letters
    while (fscanf(fp, "%s", input_word) != EOF) {
        len = strlen(input_word);
        for (int i = 0; i < len; i++) {
            letter = input_word[i];
            index = letter - 'a';
            output_word[i] = encrypted_letters[index];
        }
        output_word[len] = '\0';

        // Reverse the output word
        for (int i = 0, j = len - 1; i < j; i++, j--) {
            temp = output_word[i];
            output_word[i] = output_word[j];
            output_word[j] = temp;
        }
        // Write the reversed output word to a file
        fprintf(fp1, "%s\n", output_word);
    }
    fclose(fp);
    fclose(fp1);

    return 0;
}

int decryption() {

    char input_word[100];
    char letters[26]; 
    char encrypted_letters[26]; 
    char output_word[100];
    int len;
    char letter;
    int index;
    char temp;

    // stores the letters and the encrypted letters in 2 seperate arrays
    FILE *fp;
    fp = fopen("mapping_decryption.csv", "r");
    for (int i = 0; i < 26; i++) {
        fscanf(fp, "%c,%c\n", &letters[i], &encrypted_letters[i]);
    }
    fclose(fp);

    fp = fopen("input_decryption.txt", "r");
    FILE *fp1;
    fp1 = fopen("output_decryption.txt", "w");

    // replacing the letters with the encrypted letters
    while (fscanf(fp, "%s", input_word) != EOF) {
        len = strlen(input_word);
        for (int i = 0; i < len; i++) {
            letter = input_word[i];
            index = letter - 'a';
            output_word[i] = letters[index];
        }
        output_word[len] = '\0';

        // Reverse the output word
        for (int i = 0, j = len - 1; i < j; i++, j--) {
            temp = output_word[i];
            output_word[i] = output_word[j];
            output_word[j] = temp;
        }
        // Write the reversed output word to a file
        fprintf(fp1, "%s\n", output_word);
    }
    fclose(fp);
    fclose(fp1);

    return 0;
}


int map_decryption_error() {

    char c1;
    int flag1;
    char *character;
    char *encryption_char;

    

    FILE *fp3;
 

    fp3 = fopen("mapping_decryption.csv","r");



    // the mapping file has exactly 26 lines;

    while ((c1 = fgetc(fp3)) != EOF) {
        if (c1 == '\n') {
            flag1 += 1;
        }
    }


    if (flag1 != 26) {
        fprintf(stderr, "Error: The format of mapping file mapping_decryption.csv is incorrect\n");
        return 4;
    }


    // each line has the format <plaintext lower case letter>,<ciphertext lower case letter> (characters other than lower case letters should not be accepted)
    char line[100];
    if (fgets(line, sizeof(line), fp3) != NULL) {
        if (sscanf(line, "%c,%c", &*character, &*encryption_char) != 2 || !islower(*character) || !islower(*encryption_char))  {
            fprintf(stderr, "Error: The format of mapping file mapping_decryption.csv is incorrect\n");
            return 4;
        }
    }
   fclose(fp3);
   
}

int map_encryption_error() {

    char c2;
    int flag2;
    char *character1;
    char *encryption_char1;
    
    FILE *fp4;

    fp4 = fopen("mapping_encryption,csv","r");


    // the mapping file has exactly 26 lines;

    while ((c2 = fgetc(fp4)) != EOF) {
        if (c2 == '\n') {
            flag2 += 1;
        }
    }

    if (flag2 != 26) {
        fprintf(stderr, "Error: The format of mapping file mapping_encryption.csv is incorrect\n");
        return 4;
    }

    // each line has the format <plaintext lower case letter>,<ciphertext lower case letter> (characters other than lower case letters should not be accepted)
    
    char line2[100];
    if (fgets(line2, sizeof(line2), fp4) != NULL) {
        if ((sscanf(line2, "%c,%c", &*character1, &*encryption_char1) != 2) && (!islower(character1) || !islower(encryption_char1)) ) {
            fprintf(stderr, "Error: The format of mapping file mapping_encryption.csv is incorrect\n");
            return 4;
        }
    }
   

   
}