-
simple silly program
Hey guys,
I was just messing around making a really simple encryption type program. Reads a file in, changes some letters around (makes no effort at case sensitivity), then outputs it to another file. I have given it a good try but can't seem to get the letters to change. I'm sure it's something stupid...but I can't see it right now.
Code:
/**************************************************************************************
A program to receive a text file, then use simple encryption method to encrypt it, then
another method to decrypt it***********************************************/
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 63
#define LBUFSIZE 64
int getWord(FILE *fp, char wbuf[], int c);
void output(char lbuf[]);
FILE *encrypt;
void main(void)
{
FILE *fp;
int c = 0;
char wbuf[SIZE];
char lbuf[LBUFSIZE];
encrypt = fopen("encrypt.dat", "w");
fp = fopen("file.txt", "r");
lbuf[0] = '\0';
c = getWord(fp, wbuf, c);
while(c != EOF)
{
//add 1 for space
if((strlen(lbuf) + 1 + strlen(wbuf)) > 62)
{
output(lbuf);
strcpy(lbuf, wbuf);
}
else
{
//enough space for this word
strcat(lbuf, " ");
strcat(lbuf, wbuf);
}
c = getWord(fp, wbuf, c);
}
if (c == EOF && strlen(lbuf) > 0)
{
output(lbuf);
}
fclose(fp);
fclose(encrypt);
}
int getWord(FILE *fp, char wbuf[], int c)
{
int i = 0;
if(isalpha(c))
{
letterSwitch(c);
wbuf[i] = c;
i++;
}
c = fgetc(fp);
while(c !=EOF && isalpha(c))
{
letterSwitch(c);
wbuf[i] = c;
i++;
c = fgetc(fp);
}
wbuf[i] = '\0';
while(c != EOF && !isalpha(c))
{
c = fgetc(fp);
}
return c;
}
void output(char lbuf[])
{
printf("%s\n", lbuf);
fprintf(encrypt, "%s\n", lbuf);
}
int letterSwitch(int c)
{
switch(c)
{
case 'a':
c = '!';
case 'e':
c = '@';
case 'i':
c = '#';
case 'o':
c = '%';
case 'r':
c = '^';
case 'b':
c = '&';
case 's':
c = '*';
case 't':
c = '(';
case 'v':
c = ')';
case 'p':
c = '_';
case 'l':
c = '+';
case 'n':
c = '=';
case 'm':
c = '[';
case 'g':
c = ']';
case 'c':
c = '?';
case 'y':
c = '<';
case 'q':
c = '>';
case 'k':
c = '/';
}
return c;
}
And there it is!
-
Code:
if(isalpha(c))
{
letterSwitch(c);
wbuf[i] = c;
i++;
}
letterswitch returns the new value and does not changes the argument, you are ignoring the return value and using the original letter
wbuf[i] = letterSwitch(c);
PS.
int main(void)
-
break; is also needed after each entry in your switch statement, unless you want to always have c='/' whenever c matches any of those.
-
Thanks for the help....I actually figured both those things out as I was going along after I wrote that. But new problem. It won't finish reading the line to undo the encryption (I feel stupid calling it that). I think the problem is in getWord() the last while loop...I think maybe I'm using the wrong is...whatever function....i've been scanning through the MSDN but still haven't found what I'm looking for.....(hey...U2....LOL). And vart I'm still working on remembering that int main (our teacher didn't really pound that into us).
Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 63
#define LBUFSIZE 64
int getWord(FILE *fp, char wbuf[], int c);
void output(char lbuf[]);
int letterSwitch(int c);
int main(void)
{
FILE *fp;
int c = 0;
char wbuf[SIZE];
char lbuf[LBUFSIZE];
fp = fopen("encrypt.txt", "r");
lbuf[0] = '\0';
c = getWord(fp, wbuf, c);
while(c != EOF)
{
//add 1 for space
if((strlen(lbuf) + 1 + strlen(wbuf)) > 62)
{
output(lbuf);
strcpy(lbuf, wbuf);
}
else
{
//enough space for this word
strcat(lbuf, " ");
strcat(lbuf, wbuf);
}
c = getWord(fp, wbuf, c);
}
if (c == EOF && strlen(lbuf) > 0 && strlen(wbuf) > 0)
{
strcat(lbuf, wbuf);
output(lbuf);
}
fclose(fp);
return 0;
}
int getWord(FILE *fp, char wbuf[], int c)
{
int i = 0;
if(isascii(c) && isgraph(c))
{
c = letterSwitch(c);
wbuf[i] = c;
i++;
}
c = fgetc(fp);
while(c !=EOF && (isascii(c) && isgraph(c)))
{
c = letterSwitch(c);
wbuf[i] = c;
i++;
c = fgetc(fp);
}
wbuf[i] = '\0';
while(c != EOF && (isascii(c) && isgraph(c)))
{
c = fgetc(fp);
}
return c;
}
void output(char lbuf[])
{
printf("%s\n", lbuf);
}
int letterSwitch(int c)
{
switch(c)
{
case '!':
c = 'a';
break;
case '@':
c = 'e';
break;
case '#':
c = 'i';
break;
case '%':
c = 'o';
break;
case '^':
c = 'r';
break;
case '&':
c = 'b';
break;
case '*':
c = 's';
break;
case '(':
c = 't';
break;
case ')':
c = 'v';
break;
case '_':
c = 'p';
break;
case '+':
c = 'l';
break;
case '=':
c = 'n';
break;
case '[':
c = 'm';
break;
case ']':
c = 'g';
break;
case '?':
c = 'c';
break;
case '<':
c = 'y';
break;
case '>':
c = 'q';
break;
case '-':
c = 'k';
break;
default:
c = c;
}
return c;
}
-
"c = c" is redundant. Well, it might prevent timing attacks... :rolleyes: but this isn't encryption, it's encoding.
In addition, this might be much faster:
Code:
char lookuptable[256];
lookuptable[(int)'a'] = '!';
...
lookuptable[(int)'k'] = '-';
char reverselookuptable[256];
reverselookuptable[(int)'!']= 'a';
...
reverselookuptable[(int)'-'] = 'k';
for(i < strlen())
encodedstring[i] = lookuptable[str[i]];
for(i < strlen())
decodedstring[i] = reverselookuptable[encodedstring[i]];
-
Thanks and I guess, but that didn't really solve my problem....I'll mess with it some more and hopefully I'll come up with something. this is just to have fun so it's not that important.