11.10. Homework 10 - Cryptography on Strings

An ordinary plain text file can be encrypted in an unintelligible gibberish cipher text. To encode and decode messages, a key may be used for replacement of certain characters. Start with the readfile.c file from String Example - ReadLine for reading a file and write a C program that can encode and dencode textual data using the keys shown below. The name of the file to be read and processed should be entered as a command line argument. An additional argument of -d should be entered if the program is to decode the file.

Here are key arrays:

char encode[] = "jklmnopqrstuvwxyzabcdefghi";
char decode[] = "rstuvwxyzabcdefghijklmnopq";
char Encode[] = "TUVWXYZABCDEFGHIJKLMNOPQRS";
char Decode[] = "HIJKLMNOPQRSTUVWXYZABCDEFG";
char encodeN[] = "7890123456";
char decodeN[] = "3456789012";

Characters that are not lower case letters may be ignored. For lower case letters, the index of the encoding or decoding key may be determined as follows. Here the character being processed is represented with the identifier ch.

index = ch - 'a';

So, for example, consider the output of the following program. We begin with a character ‘h’ and after we encode the ‘h’, we have ‘q’. After the ‘q’ is decoded we are back to ‘h’.

#include <stdio.h>

int main(void)
{
    char ch;
    char encode[] = "jklmnopqrstuvwxyzabcdefghi";
    char decode[] = "rstuvwxyzabcdefghijklmnopq";
    int index;

    ch = 'h';

    index = ch - 'a';
    ch = encode[index];
    printf("%d, %c\n", index, ch);

    index = ch - 'a';
    ch = decode[index];
    printf("%d, %c\n", index, ch);

    return 0;
}

The output of this program is as follows:

7, q
16, h

Starting file: hw10_start.c