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

1. In this lab, you will create a simple encryption function that will require a

ID: 638222 • Letter: 1

Question

1. In this lab, you will create a simple encryption function that will require a sentence and a key (both
strings) as a parameter and return an encrypted version of the string. The encryption algorithm will use the
exclusive OR operator (commonly abbreviated as XOR). The general structure of the encryption is that every
position in the sentence is XOR'd with the accompanying position of the key. If the sentence is longer than the
key, you repeat the key.

For example:
Key: hello
Sentence: greetings

You would XOR:
h   e   l   l   o   h   e   l      
      g   r   e   e   t   i   n   g      
___________

Prompt the user for a keyword and a sentence of up to 100 characters. (You'll need the getline function for the
sentence) Encrypt the string with your algorithm above and print it out. Then, decrypt it by running it back
through the encryption function and print it out again. If implemented correctly, after going through the encryption
twice, you should get the original sentence.

NOTE: The XOR operator in C/C++ is ^. To XOR two chars a and b together, you would use a ^ b.

Makefile:

2. Make a makefile that will allow us to choose between Lab1 or Lab2 to select which program we want to
compile. For example:

if I wanted to compile the encryption program from Lab 2, I could specify:
make encryptor

Or if I wanted to compile Lab 1:
make mult_div


Tarball:
3. Demonstrate to your TA that you can compress/decompress a folder

Create:
tar -cvjf <filename.tar.bz2> <files you want to be in the archive>
Extract:
tar -jxvf filename.tar.bz2

-c is Create
-v is Verbose
-j is bz2 file
-f denotes we will supply a filename
-e is Extract

There is always man tar from the Linux command line. There are several suggested tar tutorial links in the
resources page of the website.

Explanation / Answer

File Encryption Program in C.

/*       WARNING : do not give the have the same sourcefile and
destinationfile, encrypting or decrypting, this bug will be worked
out as i get more interested in file encryption
*/

#include <stdio.h>

#define ENCRYPTION_FORMULA (int) Byte + 25
#define DECRYPTION_FORMULA (int) Byte - 25

int Encrypt(char * FILENAME, char * NEW_FILENAME)
{
       //printf("Loaded Encrypt
");
       FILE *inFile;                                   //Declare inFile
       FILE *outFile;                                   //Declare outFile

       char Byte;
       char newByte;
       int n;


       printf("1");
       int i=0;

       inFile = fopen(FILENAME,"rb");
       outFile = fopen(NEW_FILENAME, "w");

       if(inFile==NULL)
              printf("Error: Can't Open inFile");

       if(outFile==NULL)
       {
              printf("Error: Can't open outFile.
");
       return 1;
       }
       else
       {

              printf("File Opened, Encrypting");
              while(1)
              {
                     printf(".");


                     if(Byte!=EOF)
                     {
                            Byte=fgetc(inFile);
                     //       printf("%d",Byte);
                            newByte=Byte+25;

                            fputc(newByte,outFile);

                     }

                     else
                     {
                            printf("
End of File");
                            break;
                     }
              }
       fclose(inFile);
       fclose(outFile);

       }
}

int Decrypt (char *FILENAME, char *NEW_FILENAME)
{
       //printf("Loaded Decrypt
");
       FILE *inFile;                                   //Declare inFile
       FILE *outFile;                                   //Declare outFile

       char Byte;
       char newByte;
       printf("2");
       int i=0;

       inFile = fopen(FILENAME,"rb");
       outFile = fopen(NEW_FILENAME, "w");

       if(inFile==NULL)
              printf("Error: Can't Open inFile");

       if(outFile==NULL)
       {
              printf("Error: Can't open outFile.
");
       return 1;
       }
       else
       {

              printf("File Opened, Decrypting");
              while(1)
              {
                     printf(".");


                     if(Byte!=EOF)
                     {
                            Byte=fgetc(inFile);
                     //       printf("%d",Byte);
                            newByte=Byte-25;

                            fputc(newByte,outFile);

                     }

                     else
                     {
                            printf("
End of File");
                            break;
                     }
              }
       fclose(inFile);
       fclose(outFile);

       }
}

int main()
{
       char encFile[200];
       char newencFile[200];
       char decFile[200];
       char newdecFile[200];

       int choice;

       printf("NOTE: you must Decrypt the file with the same file
extension!!!
");
       printf("Enter 1 to Encrypt / 2 to Decrypt
");
       scanf("%d",&choice);

       switch(choice)
       {
       case 1:
                     printf("Enter the Source Filename: ");
                     scanf("%s",&encFile);
                     printf("Enter the Destination Filename: ");
                     scanf("%s",&newencFile);
                     Encrypt(encFile, newencFile);
              break;
       case 2:
                     printf("Enter the Source Filename: ");
                     scanf("%s",&decFile);
                     printf("Enter the Destination Filename: ");
                     scanf("%s",&newdecFile);
                     Decrypt(decFile, newdecFile);
              break;

       }

       return 0;
}