- May 21, 2013
- 3,656
- 60
- 91
First the code:
This is the entire main funtion, I have not included the other files required to compile.
Here is a Dropbox link to all the code: https://www.dropbox.com/sh/x65c9aiypfhmmio/AADpPFcwXWMhGsy6GbPWpov6a?dl=0
The gist is that this is an assignment where I am supposed to be be able encrypt and decrypt a text file using TEA and DES algorithms. I am also required to use two different block cipher methods. I chose Counter and Offset Feedback.
Instead of a convoluted text menu, I decided to try and get fancy by allowing options to be used.
(e)ncrypt would create two ciphertext files, one for each algorithm.
(d)ecrypt prompts the user for the encryption algorithm, and then decrypts the file.
(c)ounter specifies the counter block cipher method.
(o)ffset feedback is the other block cipher method
I've got it down to compiling with just one warning about assigning the filename to a variable, but I'm worried less about that.
Right now, my getopt code doesn't appear to work, and I can't figure it out. It looks just like so many of the examples I've used as reference.
I make this call: ./blockCode -ec Sample.txt
The print statements just tell me that it's processing option 1 for both options, and then I get an error message about not choosing to encrypt or decrypt.
I need to get over this hurdle before I can move on to actually knowing if everything else is working.
Any help as to what stupid thing I'm missing will be greatly appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "tea.h"
#include "des.h"
/****************************************************************************************
* The program must be called with two options set and a filename given.
* Example: ./blockCode -eo foo.txt
* This calls the program to (e)ncrypt using the (o)utput feedback mode a file foo.txt
* Options:
* -e : Encrypt the file; both TEA and DES are used, so two output files are
* produced.
* -d : Decrypt a file; user will be prompted for the decryption algorithm
* -c : Uses the Counter method to encrypt/decrypt
* -o : Uses the Output Feedback method to encrypt/decrypt
* When calling the file it is required that one and only one of -e and -d is called and
* one and only one of -c and -o is called.
****************************************************************************************/
int main(int argc, char* argv[])
{
extern char* optarg; // These are needed to
extern int optind; // process the options
int option, eFlag = 0, dFlag = 0, cFlag = 0, oFlag = 0; // Flags explained above
char filename[21]; // Holds the filename
char* f = filename;
int method; // Helps call the proper function for decryption
clock_t t; // This will aid in timing the executions
double time; // Variable used to display function execution time
if (argc < 2) {
printf("USAGE: ./blockCode -<e/d><c/o> <file name>\nSee README\n");
return -1;
}
while ((option = getopt(argc, argv, "edco") != -1)) {
printf("Processing option %d\n", (char)option);
switch (option) {
case 'c':
cFlag = 1;
break;
case 'd':
dFlag = 1;
break;
case 'e':
printf("Processing e option\n");
eFlag = 1;
break;
case 'o':
oFlag = 1;
break;
case '?':
printf("Improper option used\nConsult README\n");
return -1;
}
}
if (eFlag == 0 && dFlag == 0) {
printf("Did not choose to encyrpt or decrypt\nSee README\n");
return -1;
}
else if (eFlag == 1 && dFlag == 1) {
printf("Cannot encrypt and decrypt at same time\nSee README\n");
return -1;
}
else if (oFlag == 0 && cFlag == 0) {
printf("Did not choose CTR or OFB method\nSee README\n");
return -1;
}
else if (oFlag == 1 && cFlag == 1) {
printf("Cannot use CTR and OFB methods at same time\nSee README\n");
return -1;
}
if (optind == argc) {
printf("No file name given\nSee README\n");
return -1;
}
else {
f = argv[optind];
printf("Filename: %s", filename);
}
if (dFlag == 1) {
printf("Choose decryption method:\n 1. TEA\n 2. DES\nEnter the NUMBER of your choice: ");
scanf("%d", &method);
if (method < 1 || method > 2) {
printf("Only choose 1 or 2.\nExiting...\n");
return -1;
}
}
if (eFlag == 1) {
if (cFlag == 1) {
t = clock();
tea_ctr_encrypt(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("TEA CTR took %f seconds\n", time);
t = clock();
des_ctr(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("DES CTR took %f seconds\n", time);
}
else {
t = clock();
tea_ofb_encrypt(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("TEA OFB took %f seconds\n", time);
t = clock();
des_ofb(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("DES OFB took %f seconds\n", time);
}
}
else if (method == 1) {
if (cFlag == 1) {
t = clock();
tea_ctr_decrypt(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("TEA CTR took %f seconds\n", time);
}
else {
t = clock();
tea_ofb_encrypt(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("TEA OFB took %f seconds\n", time);
}
}
else {
if (cFlag == 1) {
t = clock();
des_ctr(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("DES CTR took %f seconds\n", time);
}
else {
t = clock();
des_ofb(filename);
t = clock() - t;
time = ((double) t)/CLOCKS_PER_SEC;
printf("DES OFB took %f seconds\n", time);
}
}
return 0;
}
Here is a Dropbox link to all the code: https://www.dropbox.com/sh/x65c9aiypfhmmio/AADpPFcwXWMhGsy6GbPWpov6a?dl=0
The gist is that this is an assignment where I am supposed to be be able encrypt and decrypt a text file using TEA and DES algorithms. I am also required to use two different block cipher methods. I chose Counter and Offset Feedback.
Instead of a convoluted text menu, I decided to try and get fancy by allowing options to be used.
(e)ncrypt would create two ciphertext files, one for each algorithm.
(d)ecrypt prompts the user for the encryption algorithm, and then decrypts the file.
(c)ounter specifies the counter block cipher method.
(o)ffset feedback is the other block cipher method
I've got it down to compiling with just one warning about assigning the filename to a variable, but I'm worried less about that.
Right now, my getopt code doesn't appear to work, and I can't figure it out. It looks just like so many of the examples I've used as reference.
I make this call: ./blockCode -ec Sample.txt
The print statements just tell me that it's processing option 1 for both options, and then I get an error message about not choosing to encrypt or decrypt.
I need to get over this hurdle before I can move on to actually knowing if everything else is working.
Any help as to what stupid thing I'm missing will be greatly appreciated.