Exercise 14
Info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting, and submit on Gradescope to check your solution. You are encouraged to work at home to complete what you do not get through today.

Learning Objectives

Reinforces concepts learned in today meeting and earlier:

  • arrays & strings
  • function writing
  • bit operations (AND, OR, XOR, SHIFT)

Part 1

Pull the starter code for this exercise from the public repo by taking the following steps:

  1. Log into an undergraduate cluster computer. Update the course public repo with a git pull command.

  2. Confirm that you can see the template files for today’s exercise by typing ls exercises/ex14 – you should see a file named encrypt.c inside.

  3. Navigate to your personal repo for the course and make a directory for today’s exercise. Copy encrypt.c source file from the public class repository (in the */exercises/ex14/* directory) into your personal repo’s directory for this exercise.

Part 2

The file encrypt.c contains some comments on each step you need to complete. Here is a description of what you need to complete:

Sam and Chloe are friends. They want to encrypt their conversation messages so that they can keep their communications confidential. Your job is to help them implement the encryption mechanism they have in mind:

Example: Let us say the plain message is 1001011 and n = 4.

      1 0 0 1 0 1 1    shift 0 (plain message)
    1 0 0 1 0 1 1 0    shift 1
  1 0 0 1 0 1 1 0 0    shift 2
1 0 0 1 0 1 1 0 0 0    shift 3 
------------------------------
1 1 1 0 1 0 1 0 0 1 ← final encoded message (XORed) 

Write a C program to perform this encryption. If the entered message contains anything other than 0s/1s and/or n<=0, your program should print a sensible message and exit with code 1.

  1. Open encrypt.c for editing using emacs or vim.

  2. Read the source code and the comments. Your instructions are in the comments.

  3. Modify the source files and test by:

    • Compiling with gcc -Wall -Wextra -std=c99 -pedantic encrypt.c -o encrypt or using your alias: gccc encrypt.c -o encrypt.
    • Execute ./encrypt and test your code.