0% found this document useful (0 votes)
6 views

Mini Project 1 Report

Uploaded by

jvandenh3566
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Mini Project 1 Report

Uploaded by

jvandenh3566
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Mini Project 1

Mini Project 1 Report

Evan Laurin and Jakob van den Heuvel

Group: 81(Friday)

University of Guelph

Course Number: Engg*1410

Date: Nov 5, 2023


Mini Project 1 2

Problem Statement

The problem for this project was to design a C program capable of encrypting and decrypting

text files provided by the user with command line arguments and using an encryption algorithm given in

the project documentation.

Constraints

Some of the major constraints included having to read in the files line by line, having to use the

given encryption scheme and having to complete it in the given time frame.

Problem Solution

Pseudocode

Main:
Include necessary libraries and functions
Declare the main function with arguments numArgs and args
If number of arguments is 2
Print "Encrypting args[1]"
Call the 'encrypt' function with the file opened using 'fopen(args[1], "r")' and 'args[1]' as
arguments
If args[1] is equal to "-E"
Print "Encrypting args[2]"
Call the encrypt function with the file opened using 'fopen(args[2], "r")' and 'args[2]' as
arguments
If args[1] is equal to "-D"
Print "Decrypting args[2]"
Call the decrypt function with the file opened using 'fopen(args[2], "r")' and 'args[2]' as
arguments
Functions:
Function changeExt(filePath, newExt):
pathEnd = pointer to the end of filePath
While (pathEnd points to a character and it's not a period)
Move pathEnd one character to the left
End While
If (pathEnd doesn't point to a period)
Set pathEnd to the end of filePath
Set the character pointed by pathEnd to null
Concatenate newExt to filePath
Mini Project 1 3

*************************************************************************************
Function hexToInt(c):
c -= 48
If c is greater than 9
c -= 7
Return c
*************************************************************************************
Function encrypt(enFile, filePath):
newFilePath = filePath
newExt = ".crp"
Call changeExt(newFilePath, newExt)
newFile = OpenFile(newFilePath, "w")
inBuffer = CreateArray(255)
While (Read a line from enFile into inBuffer)
For i from 0 to length of inBuffer
If inBuffer[i] is a tab character (ASCII 9)
Write "TT" to newFile
Else If inBuffer[i] is a newline character (ASCII 10)
Write a newline character to newFile
Else
inChar = inBuffer[i]
inChar -= 16
If inChar < 32
Write hexadecimal representation of (inChar - 32 + 144) to newFile
Else
Write hexadecimal representation of inChar to newFile
End If
End For
End While
Close newFile
*************************************************************************************
Function decrypt(deFile, filePath):
newFilePath = filePath
newExt = ".txt"
Call changeExt(newFilePath, newExt)
newFile = OpenFile(newFilePath, "w")
inBuffer = CreateArray(255)
While (Read a line from deFile into inBuffer)
length = length of inBuffer
For i from 0 to length by 2
If inBuffer[i] is 'T'
Write a tab character to newFile
Else If inBuffer[i] is a newline character
Write a newline character to newFile
Decrement i by 1
Else
outChar = 16 * hexToInt(inBuffer[i]) + hexToInt(inBuffer[i + 1])
outChar += 16
Mini Project 1 4

If outChar is greater than 127


Write the character corresponding to (outChar - 144 + 32) to newFile
Else
Write the character corresponding to outChar to newFile
End If
End For
End While
Close newFile
*************************************************************************************
Flowchart

Main:

Functions:

hexToInt:
Mini Project 1 5

changeExt:
Mini Project 1 6

encrypt/decrypt:
Mini Project 1 7
Mini Project 1 8

Overview And Justification

The way we designed our system was to have a very simple main function whose purpose was

to read the arguments of the command lines and pass the file provided into the encryption or

decryption function. By creating separate functions for each part of our system we were able to test

each part individually and debug our program more easily. For the encryption and decryption function

they both start by creating a new file with the changed file extension to write the new file contents. This

saves time and memory by not requiring the new text to be stored in an array first and instead directly

prints to the new file. The function then reads the input file line by line storing its contents into a buffer

and then applying the encryption or decryption algorithm and writing it to the new file. Our hex to int

function takes in a single hexadecimal digit and returns the corresponding integer value. The reason we

designed this as a separate function was to improve the readability of our program and to reduce

repetition. The final function in our program is the change extension function which takes in two

character pointers corresponding to the old file path and the new file extension. It decrements the

character pointer starting at the end of the file path looking for the first instance of a period and

appends the new file extension there. If a period isn’t found it will just append the file extension to the

end of the file path.


Mini Project 1 9

Error Analysis

Encryption:

Test Case 1:

Input:

text.txt:

Hello There how are you?


My name is Sean Clarke. I like software!

.\cryptoMagic -E text.txt

Output:

text.crp:

38555C5C5F80445855625580585F678051625580695F652F80
3D69805E515D55805963804355515E80335C51625B558ETT39805C595B5580635F56646751625581

Test Case 2:

Input:

text:

Hello There how are you?


My name is Sean Clarke. I like software!

.\cryptoMagic text

Output:

text.crp:

38555C5C5F80445855625580585F678051625580695F652F80
3D69805E515D55805963804355515E80335C51625B558ETT39805C595B5580635F56646751625581
Mini Project 1 10

Decryption:

Test Case 1:

Input:

text.crp:

4458596380555E5362696064595F5E80635358555D55805963806062556464698067555962548E
39635E87648059642F812F

.\cryptoMagic -D text.crp

Output:

text.txt:

This encryption scheme is pretty weird.


Isn't it?!?

Test Case 2:

Input:

text:

4458596380555E5362696064595F5E80635358555D55805963806062556464698067555962548E
39635E87648059642F812F

.\cryptoMagic -D text

Output:

text.txt:

This encryption scheme is pretty weird.


Isn't it?!?

You might also like