Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIS 111 – C Programming - Programming Project Text File Encryption Utility – Aff

ID: 3576063 • Letter: C

Question

CIS 111

C Programming

- Programming Project

Text File Encryption Utility

Affine Cipher

Overview

In this assignment, the student will write a C prog

ram that

When completing this assignment, the student should

demonstrate mastery of

the following concepts:

Cryptography

General Concepts (text cleaning, encryption, decryption)

Cryptography

Affine Cipher

File I/O

UI Design

Menu Interfaces

Mathematics

Prime/Coprime

Mathematics

Modular Inverses

Mathematical Utility Function Design

String Manipulation and Formatting

Decision Making Logic

Looping Logic

Assignment

Write a C program that provides an interface for th

e use of the Affine

Cipher.

Recall that the Affine Cipher uses the following mathematical

formulas to encrypt and decrypt messages:

E(x) = Ax + B (mod N) <= ENCRYPTION FUNCTION

D(x) = A

-1

(x

B) (mod N) <= DECRYPTION FUNCTION

Note:

A and N must be coprime. This means they share no

prime factors.

A

-1

is the inverse of A (mod N). This means A * A

-1

= 1 (mod N)

In this assignment, you will explore some of the ba

sic ideas of

cryptography.

In cryptography, it is our job to provide a mechanism that

takes a message and confuscates it in such a way so

that it is no longer

readable to the common person.

This

encrypted

message can be passed back

and forth between two parties in the open and other

users will not be able to

determine its meaning. This provides a means for t

he two parties to secretly

exchange information. The intended user knows the m

ethod used to obscure the

message and

ca

n

decrypt

it to reveal the original message. The methods use

d

to encrypt and decrypt the messages are called

ciphers

.

You will be writing a program that allows its user

to encrypt and decrypt

text files. Modern cryptography uses extremely comp

licated ciphers that would

take months of study to review. Instead of studying

modern techniques, we

will implement a simple, archaic technique called t

he Affine Cipher (see

class notes for a detailed description of the Affin

e Cipher).

Recall that A and B are mathematical constants used

to determine the

key

used by the Affine Cipher. The two parties exchangi

ng information would agree

upon a key upfront so they know how to encrypt and

decrypt the messages

mathematically. N is the size of the alphabet we ar

e using.

In cryptography,

we typically remove all spaces, punctuation and cas

e information from the

letters.

This means we must transform input files so that they do not

contains these invalid characters.

All

message will become a series of

lowercase letters, which gives us 26 unique symbols

we need to deal with.

This gives us an alphabet size of N = 26.

Example:

Plain Text : This is an English sentence. It contai

ns invalid characters!

Clean PT : thisisanenglishsentenceitcontainsinvalid

characters

Your pr

ogram should provide a user-friendly interface for

encrypting and

decryption messages with this cipher. The messages

will be in text files in

the same directory as the program. The program shou

ld provide interface

options that allow the user to select the input and

output files and choose

the key used by the Affine Cipher to encrypt and de

crypt data. Finally, the

program should have a menu option that makes a syst

em call to display the

contents of the current directory.

Your program should provide the user with a menu co

ntaining the following

options:

Select Input File

Select Output File

Select Affine Cipher Key

Encrypt Input File

Decrypt Input File

Get Directory Listing

When designing your program, you will want to keep

the following things in

mind:

The

data read from the input file must be transformed

(see above

example) in case it contains spaces, punctuation, u

ppercase letters,

or other invalid characters.

The E

nglish sentence

The quick brown fox jumped over the lazy

dogs.

cont

ains all the letters in the English alphabet. Use

it

when testing your encryption and decryption functio

ns to make sure

they are

doing the proper operations on all the English lett

ers.

Not all values are valid selections for A and B.

Since the user is

selecting the key used by the cipher, you should be

checking to make

sure the entered values are valid.

There is a difference between A and A

-1

. When a user enters a valu

e

for A,

immediately calculate the value of A

-1

so your program will

have it ready to use for decrypting messages.

The DOS command for displaying the contents of the

current directory

is

dir /p

.

DOS commands can be issued by your program with the

system()

function found in

stdlib.h

(i.e.

system(

dir /p

);

would

generate the

curr

ent directory listing).

Check to make sure the filenames provided by the us

er are valid

before the program reads/writes data to/from them.

The following

code demonstrates the technique you should use to p

erform a check on

the file handle.

FILE* fileHandle = NULL;

fileHandle = fopen(

someFile.txt

,

r

);

if(fileHandle == NULL) {

printf(

The file could not be opened for reading.

);

return ERROR;

}

Aside from these restrictions, you have full creati

ve license. Please

organize your code in an intelligent manner and pro

vide comments and

functions where appropriate.

When preparing your sample output, please encrypt a

text file that contains

the sentence:

The quick brown fox jumped over the lazy dogs.

Encrypt the message with the Affine Cipher key (19,

4). The sample output

below illustrates a properly written program that e

ncrypts and decrypts this

message with that key. Please check the results of

your program against the

results of the program below to make sure everythin

g is working correctly.

Sample

Run Text Listing

Note: The file input.txt contains: The quick brown

fox jumped over the lazy

dogs.

---

CIS 111 - Affine Cipher Program - Author: Mark Den

cler -----

---------------------------------------------------

-------------

The current input file is : input.txt

The current output file is: output.txt

Affine Cipher - e(x) = (Ax + B)(mod 26)

A = 1

A^(

-1) = 1

B = 1

---------------------------------------------------

-------------

Please select an option:

1.) Select input file name

2.) Select output file name

3.) Select affine cipher key

4.) Encrypt input file

5.) Decrypt input file

6.) Get directory listing

7.) Exit

:3

Enter a value for A: 19

Enter a value for B: 4

Command Executed... Press Any Key to Continue

---

CIS 111 - Affine Cipher Program - Author: Mark Den

cler -----

---------------------------------------------------

-------------

The current input file is : input.txt

The current output file is: output.txt

Affine Cipher - e(x) = (Ax + B)(mod 26)

A = 19

A^(

-1) = 11

B =

4

---------------------------------------------------

-------------

Please select an option:

1.) Select input file name

2.) Select output file name

3.) Select affine cipher key

4.) Encrypt input file

5.) Decrypt input file

6.) Get directory listing

7.) Exit

:4

Plain Text : The quick brown fox jumped over the la

zy dogs.

Sanitized Plain Text : thequickbrownfoxjumpedoverth

elazydogs

Cipher Text : bhcwuaqmxpkgrvkztuydcjkncpbhcfelsjkoi

Command Executed... Press Any Key to Continue

---

CIS 111 - Affine Cipher Program - Author: Mark Den

cler -----

---------------------------------------------------

-------------

The current input file is : input.txt

The current output file is: output.txt

Affine Cipher - e(x) = (Ax + B)(mod 26)

A = 19

A^(

-1) = 11

B = 4

---------------------------------------------------

-------------

Please select an option:

1.) Select input file name

2.) Select output file name

3.) Select affine cipher key

4.) Encrypt input file

5.) Decrypt input file

6.) Get directory listing

7.) Exit

:1

Input File Name: output.txt

Command Executed... Press Any Key to Continue

---

CIS 111 - Affine Cipher Program - Author: Mark Den

cler -----

---------------------------------------------------

-------------

The current input file is : output.txt

The current output file is: output.txt

Affine Cipher - e(x) = (Ax + B)(mod 26)

A = 19

A^(

-1) = 11

B = 4

---------------------------------------------------

-------------

Please select an option:

1.) Select input file name

2.) Select output file name

3.) Select affine cipher key

4.) Encrypt input file

5.) Decrypt input file

6.) Get directory listing

7.) Exit

:2

Output File Name: input.txt

Command Executed... Press Any Key to Continue

---

CIS 111 - Affine Cipher Program - Author: Mark Den

cler -----

---------------------------------------------------

-------------

The current input file is : output.txt

The current output file is: input.txt

Affine Cipher - e(x) = (Ax + B)(mod 26)

A = 19

A^(

-1) = 11

B = 4

---------------------------------------------------

-------------

Please select an option:

1.) Select input file name

2.) Select output file name

3.) Select affine cipher key

4.) Encrypt input file

5.) Decrypt input file

6.) Get directory listing

7.) Exit

:5

Cipher Text : bhcwuaqmxpkgrvkztuydcjkncpbhcfelsjkoi

Plain Text : thequickbrownfoxjumpedoverthelazydogs

Command Executed... Press Any Key to Continue

---

CIS 111 - Affine Cipher Program - Author: Mark Den

cler -----

---------------------------------------------------

-------------

The current input file is : output.txt

The current output file is: input.txt

Affine Cipher - e(x) = (Ax + B)(mod 26)

Explanation / Answer

#include <stdlib.h>

#include <time.h>

#include <stdio.h>

#define bool int

int menu();

int euclid(int, int);

void affine_do(char[], char[], int, int);

void affine(int, int, FILE*, FILE*);

bool getDecrypt(int a, int b, int& c, int& d);

int main() {

     int res = menu();

     while (res != 4) {

          if (res == 1) {

              char inFile[100];

              char outFile[100];

              printf("Enter input file name.");

              scanf("%s", &inFile);

              printf("Enter output file name.");

              scanf("%s", &outFile);

              int a, b;

              printf("Enter Affine cipher values of a and b.");

              scanf("%d %d", &a, &b);

              affine_do(inFile, outFile, a, b);

          }

          else if (res == 2) {

             

              int a, b;

              printf("Enter Affine cipher values of a and b.");

              scanf("%d %d", &a, &b);

              int c, d;

              if (getDecrypt(a, b, c, d))

                   printf("Decrypting keys are %d and %d ", &a, &b);

              else

                   printf("Sorry, those are not valid keys. ");

          }

          else if (res == 3)

              system("dir /p");

          else if (res > 4)

              printf("Not a valid menu choice. Try again.");

          res = menu();

     }

}

int menu() {

     int pick;

     printf("Pick your menu choice : ");

     printf("1. Apply affine cipher to a file ");

     printf("2. Find corresponding decrypting keys for the affine cipher ");

     printf("3. Get directory listing ");

     printf("4. Quit. ");

     scanf("%d", &pick);

     return pick;

}

void affine_do(char inFile[], char outFile[], int a, int b) {

    

     FILE *input, *output;

    

     if (euclid(a, 26)<0)

          printf("That is not a valid affine shift key.");

     else {

    

          input = fopen(inFile, "r");

          output = fopen(outFile, "w");

     }

     affine(a, b, input, output);

     fclose(input);

     fclose(output);

}

void affine(int a, int b, FILE *input, FILE *output) {

     char c;

     while (fscanf(input, "%c", &c) != EOF) {

    

          if (c >= 65 && c <= 90)

              c = char((a*((int)c - 65) + b) + 65);

          else if (c >= 97 && c <= 122)

              c = char((a*((int)c - 97) + b) + 97);

    

          fprintf(output, "%c", c);

     }

}

bool getDecrypt(int a, int b, int& c, int& d) {

    

     int inverse = euclid(a, 26);

    

     if (inverse < 0) {

          c = 0;

          d = 0;

          return false;

     }

     else {

          c = inverse;

          d = (-1 * b*c);

    

          if (d < 0)

              d += 26;

     }

     return true;

}

int euclid(int x, int n) {

     int t0 = 0, t1 = 1, t2;

     int r0, r1, r2, q;

     r0 = n;

     r1 = x;

     r2 = n%x;

     q = n / x;

     t2 = (t0 - q*t1 + 100 * 26) % 26;

     if (r1 == 1)

          t2 = 1;

    

     while (r2 != 0) {

    

          int temp = r2;

          q = r1 / r2;

          r2 = r1 - q*r2;

          r1 = temp;

    

          if (r2 != 0) {

              int temp2 = t2;

              t2 = (t1 - q*t2 + 100 * 26) % 26;

              t1 = temp2;

          }

     }

     if (r1 == 1)

          return t2;

     else

          return -r1;

}