Demonstrating Program Arguments Bitwise Operators Header Fil
Demonstrating
Program Arguments
Bitwise Operators
Header Files
String Manipulation
Standard Libraries
Overview
In this lab, you will write a program that accepts parameters from the command line and encrypts or decrypts a message.
I feel obligated to mention that these are not secure encryption methods and many would only call them obfuscation. CSE 5351 Introduction to Cryptography covers various real world cryptographic algorithms.
Outline
Your program should be separated into 3 .c files:
o caesar.c – You should implement the Caesar Cipher in caesar.c.
o xor.c – You should implement the XOR Cipher in xor.c
o lab4.c – Your main function and command line argument parsing code should go in lab4.c. This should call the functions implemented xor.c or caesar.c to perform the encryption or decryption.
Both caesar.c and xor.c should have an associated header file (caesar.h and xor.h). These header files should include declarations for the caesar and xor algorithms that are implemented in their respective .c files. Lab4.c should include these header files.
Your program should be called like: o ./lab4 [caesar|xor] [-r num_caesar_rotate] [-k xor_key] message
o The first parameter selects which encryption algorithm to use. Your lab should support both caesar and xor as described in the later sections.
o Both -r and -k are optional parameters. If they are omitted, the program should still perform correctly with default values. If they are specified, the program should use the value provided instead of the default.
Caesar Cipher (40%)
The Caesar Cipher is a type of substation cipher that replaces each letter of the alphabet with a letter shifted from o https://en.wikipedia.org/wiki/Caesar_cipher
Uppercase letters should remain uppercase; Lowercase letters should remain lowercase.
If an input character is not a valid letter, it should remain the same in the output
If no r parameter is specified, assume 13 should be used (ROT13)
Examples:
o ./lab4 caesar caesar
pnrfne
o ./lab4 caesar PNRFNE
CAESAR
o ./lab4 caesar -r 3 “Hello, World!”
Khoor, Zruog!
o ./lab4 caesar -r 3 “Khoor, Zruog!”
Hello, World! XOR
Encoding (40%)
XOR is a fundamental operation in many cryptographic algorithms
o https://en.wikipedia.org/wiki/XOR_cipher
The basic property of XOR that:
o Plaintext XOR Key = Ciphertext
o Ciphertext XOR Key = Plaintext
In this lab, we will be using a 1byte key, specified on the command line in hexadecimal (Hint: strtol).
If no XOR key is specified with the -k option, use 0xFF.
Examples:
o ./lab4 xor -k 0x22 Hello
jGNNM
o ./lab4 xor -k 0x22 jGNNM
Hello
o ./lab4 xor \"Hello World\" | xargs -0 ./lab4 xor -k FF
Hello World
NOTE: xargs is a standard Linux command line utility. In this invocation it is taking the standard output from the first call the lab4 and supplying it as the last parameter to the second call to lab4. In this way it is verifying both the encryption and decryption steps by getting “Hello World” as output of the second call to lab4.
Note that when performing XOR you may encrypt to values that are not printable ASCII. To examine the output, you can redirect standard output from your program to hexdump:
o /lab4 xor -k 0xFF \"Hello World\" | hexdump –C
o 00000000 b7 9a 93 93 90 df a8 90 8d 93 9b 0a |............|
Command Argument Validation (20%)
Users are stupid. [Citation Needed] Assume the user will misuse and abuse your command line arguments.
o NOTE: For this portion you DO have to design and check input values. This is an exception to the general lab guidelines. If you do not properly validate input, you can still receive credit for the first two sections.
When a user supplies invalid input parameters or options your program should provide useful error messages that describe why the input was wrong.
There is no single correct error message for each condition. This section will be graded on how descriptive the error message is about the cause of the error.
You should add a PrintUsage() function that describes the intended output and valid arguments. After each error message your program should also use this function to print out the intended usage.
Examples (Not an exhaustive list of error cases or messages you must print):
o ./lab4 aes
Error: “aes” is not a recognized encryption algorithm. Please selection from: caesar, xor … …
Output from PrintUsage()
o ./lab4 caesar –q message
Error: “q” is not a recognized option for the caesar encryption algorithm. … …
Output from PrintUsage()
Compiling
Your source must compile with:
gcc -o lab4 -Wall lab4.c caesar.c xor.c
You can run your program in GDB by first compiling with symbols (g) then invoking GDB:
gcc -g -o lab4 -Wall lab4.c caesar.c xor.c gdb ex run ./lab4
Solution
lab4.c
#include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>
 #include \"caesar.h\"
 #include \"xor.h\"
void common_mistakes(int, char**);
 void print_usage(int, int, char**);
int main(int argc, char *argv[]) {
   common_mistakes(argc, argv);
    // calls the fucntion common_mistakes
   
    if (strcmp(argv[1], \"caesar\") == 0) {
    // checks if argument in command line is caesar
        caesar(argc, argv);
        // if so calls fucntion caesar
    } else if (strcmp(argv[1], \"xor\") == 0) {
    // checks if argument in command line is xor
        xor(argc, argv);
        // if so calls fucntion xor
    }
return 0;
}
void common_mistakes(int argc, char** argv) {
       if (strcmp(argv[1], \"caesar\") != 0 && strcmp(argv[1], \"xor\") != 0) {
        // checks if first argument is neither xor nor caesar
            print_usage(1, argc, argv);
            // if so calls function print usage with error usage 1
        }
       
       if (argc > 3) {
        // if the number of arguments is greater than 3
            if (strcmp(argv[1], \"caesar\") == 0) {
            // checks if the first coomand line arg is caesar
                if (strcmp(argv[2], \"-r\") != 0) {
                    // checks if the following argument is -r or not
                    print_usage(2, argc, argv);
                    // if it is not then calls function print usage with
                    // error message 2
                }
               int i = atoi(argv[3]);
                // changes char after -r to an integer
               if (isdigit(i)) {
                // checks if integer is a digit or not
                    print_usage(3, argc, argv);
                    // if not then calls function print usage with
                    // error message 3
                }
           } else if (strcmp(argv[1], \"xor\") == 0) {
            // chekcs if the first command line arg is xor
                if (strcmp(argv[2], \"-k\") != 0) {
                // if so checks if following arg is -k or not
                    print_usage(2, argc, argv);
                    // if not then calls function print usage with error
                    // message 2
                }
            }  
        }
 }  
void print_usage(int x, int argc, char** argv) {
   if (x == 1) {
    // checks if x equals 1
        printf(\"Error: \\\"%s\\\" is not a rcognized encryption algorthm.\", argv[1]);
        printf(\"\ Please select from: caesar, xor\ \");
        // if so prints error messages and proper usage
    } else if (x == 2) {
    // checks if x equals 2
        printf(\"Error: \\\"%s\\\" is not a recognized option for the %s ecryption algorthm.\ \", argv[2], argv[1]);
        if (strcmp(argv[1], \"caesar\") == 0) {
        // checks if command line arg is caesar
            printf(\"Please choose \\\"-r\\\" or nothing at all.\ \");
            // if so prints proper usage message
        } else {
        // else must be xor
            printf(\"Please choose \\\"-k\\\" or nothing at all.\ \");
            // prints proper usage message
        }
    } else if (x == 3) {
    // checks if x equals 3
        printf(\"Error: please input a digit after \\\"-r\\\".\ \");
        printf(\"Example: ./lab4 caesar -r 5 Hello.\ \");
        // if so prints error messgages and proper usage
    }
       
    exit(EXIT_FAILURE);
    // proceeds to exit the fucntion
 }
 caesar.c
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include \"caesar.h\"
void caesar(int argc, char *argv[]) {
   const char alphabet[] = \"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\";
    const char ALPHABET[] = \"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ\";
    // strings of the alphabet, both lowercase and uppercase
   
    int start = 2;
    // number of args in to test for -r or not
   if (strcmp(argv[start], \"-r\") == 0) {
        // checks if the command line arg is -r or not
        start++; // increments start
        argc = atoi(argv[start]);
       if (argc > 26 || argc < -26) {
            // checks if the argc is greater than or
            // less than 26 or -26, and if so mods it
            argc = argc % 26;
        }  
       start++; // increments start
   
    } else if (argc == 3) {
        // checks if the numbers of args are 3, and if
        // so sets the argc to 13
        argc = 13;
    }
   int size = strlen(argv[start]);
    // gets the length of the word passed
    char word[size];
    // mallocs memory the size of the word
    strcpy(word, argv[start]);
    // copies the word to the malloced string word
    int i, j;
    // two incrementing ints
   for (i = 0; word[i] != \'\\0\'; i++) {
        for (j = 0; j < 26; j++) {
           if (argc > 0) {
            // checks if the argc is positive or not
                if (word[i] == alphabet[j]) {
                // checks if the letter matches the letter in alphabet
                    word[i] = alphabet[j + argc];
                    // increments the letter in word by argc
                    break; // breaks the inner loop
                } else if (word[i] == ALPHABET[j]) {
                // if the word is capital, does the same as above
                    word[i] = ALPHABET[j + argc];
                    // increments the letter in word by argc
                    break; // breaks the inner loop
                }  
            }
           if (argc < 0) {
            // checks if the argc is negative or not
                if (word[i] == alphabet[j]) {
                // checks if the leter matches the letter in alphabet
                    word[i] = alphabet[j + 26 + argc];
                    // decrements the letter in word by argc
                    break; // breaks the inner loop
                } else if (word[i] == ALPHABET[j]) {
                // if the word is capital, does the same as above
                    word[i] = ALPHABET[j + 26 + argc];
                    // decrements the letter in word by argc
                    break; // breaks the inner loop
                }
            }  
        }
    }  
       
    printf(\"%s\ \", word);
 }
caesar.h
void caesar(int argc, char * argv[]);
 // declare the function caesar
xor.c
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include \"xor.h\"
void xor(int argc, char *argv[]) {
   char * backup = \"0xff\";
    // default hex key
    int num;
    // possible hex key
   int start = 2;
    // check place of command line spot
   if (strcmp(argv[start], \"-k\") == 0) {
    // checks if the command line argument is -k or not
        start++; // increments start
        char * hex = argv[start];
        // gets the hex key from command line
        num = (int) strtol(hex, NULL, 16);
        // uses function strtol on hex with base 16
        start++; // increments start
    } else {
        num = (int) strtol(backup, NULL, 16);
        // uses function strtol on hex with base 16
    }  
   int size = strlen(argv[start]);
    // gets the length of the meesgae passed in the command line
    int i;
    // incrementing int
    char word[size];
    // array of characters the size of the message
    char xor[size];
    // array of characters the size of the message
    strcpy(word, argv[start]);
    // copies the message from command line to array word
   for (i = 0; i < size; i++) {
        xor[i] = word[i] ^ num;
        // xors each letter in word by key num
    }
   printf(\"%s\ \", xor); // prints the word
 }  
 xor.h
void xor(int argc, char * argv[]);
 // delcare the function cor







