Encryption program.... just doesn't work.
Yeah.....
It compiles fine, so I don't know what's wrong with it.
Don't steal my source.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
//Pretty self-explanatory.
//Reminders: 0 is success, other is error.
//(I get those mixed up.....)
//1 is true, 0 is false
void showusage(char* progname) {
printf(
"Usage: %s [options] file\n"
" -e = encrypt file\n"
" -d = decrypt file\n"
" -v = value to use for encryption / decryption.\n"
" default is 3.\n"
" -h = print this help page\n"
" OPTIONS ARE CASE SENSITIVE\n"
" Example:\n"
" %s -e -v 25 blah.txt\n", *progname, *progname);
}
int encrypt(int blah, int val) {
if ((blah + val) > 255) {
return (blah + val - 255);
}
else {
return blah + val;
}
}
int decrypt(int blah, int val) {
if ((blah - val) < 0) {
return (blah - val + 255);
}
else {
return blah - val;
}
}
int main(int argc, char *argv[]) {
int value, action;
FILE *file;
int optnum;
char readfromfile, writetofile;
fpos_t position;
for (optnum = 1; argc - 2 > optnum; optnum++) {
switch (argv[optnum][2]) {
case 'e':
action = 1;
break;
case 'E':
action = 1;
break;
case 'd':
action = 0;
break;
case 'D':
action = 0;
break;
case 'v':
value = atoi(argv[(optnum + 1)]);
optnum++;
break;
case 'V':
value = atoi(argv[(optnum + 1)]);
optnum++;
break;
case 'h':
showusage(argv[0]);
break;
case 'H':
showusage(argv[0]);
break;
default:
showusage(argv[0]);
break;
}
}
file = fopen(argv[(argc - 1)], "w+");
if (file == NULL) {
showusage(argv[0]);
exit(1);
}
while (!feof(file)) {
fgetpos(file, &position);
readfromfile = fgetc(file);
fsetpos(file, &position);
if (action == 1) {
writetofile = encrypt((int)readfromfile, value);
}
if (action == 0) {
writetofile = decrypt((int)readfromfile, value);
}
fputc(writetofile, file);
}
if (fclose(file) == 0) {
return 0;
exit(0);
}
return 1;
}
...yeah....