Hey guys, i needed some help with this encryption-decryption program that i am trying to write, as you will notice, i am not very skilled or experienced but i want to grow naturally, so it would be great if you guys could point out the mistakes rather than suggest smth totally new or advanced but any sort of help is appreciated....also just to explain what it does...it basically reads text from a file and using the mod function, encrypts it into gibberish and vice versa...

# include <stdio.h>
# include <stdlib.h>
#include <math.h>

char *ref[52]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

void encrypt ();
void decrypt ();

void main()
{
	int choice;

	printf("\nPlease choose what would you like to do.\n");
	printf("1. Encrypt a message.\n");
	printf("2. Decrypt a message.\n");
	printf("3. Exit the program.\n\n");

	scanf("%d",&choice);

	switch(choice)
	{
	case 1:
		encrypt();
		break;
	case 2:
		decrypt();
		break;
	case 3:
		printf("Thank you for using the program.\n");
		break;
	default:
		break;
	}
}


void encrypt()
{
	int key,i,result;
	char temp;

	FILE *spt;
	FILE *upt;
	spt=fopen("source.txt","r");
	upt=fopen("result.txt","w");

	printf("\nPlease enter the key for encryption.\n");
	scanf("%d",&key);

	if (spt==NULL)
	{
		printf("ERROR:File could not be opened\n");
	}
	else
	{
		while (!feof(spt))
		{
			fscanf(spt,"%c",&temp);

			if(temp==' ')
			{
				fprintf(upt,"%c",' ');
			}
			else
			{

				for (i=0;i<=52;i++)
				{
					if (temp==*ref[i])
					{
						break;
					}
				}

				result=(i+key)%52;
				printf("\n%d",result);

				fprintf(upt,"%c",*ref[result]);
			}
		}

	}
	
	fclose(spt);
	fclose(upt);
}



void decrypt()
{
	int key,i,result;
	char temp;

	FILE *spt;
	FILE *upt;
	spt=fopen("result.txt","r");
	upt=fopen("result2.txt","w");

	printf("\nPlease enter the key for decryption.\n");
	scanf("%d",&key);

	if (spt==NULL)
	{
		printf("ERROR:File could not be opened\n");
	}
	else
	{
		while (!feof(spt))
		{
			fscanf(spt,"%c",&temp);

			if(temp==' ')
			{
				fprintf(upt,"%c",' ');
			}
			else
			{

				for (i=0;i<=52;i++)
				{
					if (temp==*ref[i])
					{
						break;
					}
				}

				result=abs(i-key)%52;
				printf("\n%d\n",result);

				fprintf(upt,"%c",*ref[result]);
			}
		}

	}
	
	fclose(spt);
	fclose(upt);
}





;

Thanks alot in advance....

Nice, I compiled something similiar to this in VB once. Looks promising, have you compiled it and run it? Did you get any errors?

Why the one char strings in refs[], instead of just single char's? Looks quite odd.

morten@debian504intel:~$ gcc encryption-decryption.c
encryption-decryption.c: In function ‘main’:
encryption-decryption.c:11: warning: return type of ‘main’ is not ‘int’
morten@debian504intel:~$ gcc -Wall encryption-decryption.c
encryption-decryption.c:10: warning: return type of ‘main’ is not ‘int’
morten@debian504intel:~$ ./a.out

Please choose what would you like to do.
1. Encrypt a message.
2. Decrypt a message.
3. Exit the program.

1

Please enter the key for encryption.
kj
ERROR:File could not be opened
Segmentation fault
morten@debian504intel:~$

I just tried it, could you send me a note how it is supposed to work?

I love and prefer openpgp, which works quite easy.

morten@debian504intel:~$ echo "Attack at Dawn." > /tmp/plaintext.txt
morten@debian504intel:~$ cat /tmp/plaintext.txt
Attack at Dawn.
morten@debian504intel:~$ cd /tmp/
morten@debian504intel:/tmp$ gpg -c plaintext.txt
morten@debian504intel:/tmp$ ls -l plaintext.txt*
-rw-r--r-- 1 morten morten 16 2010-09-08 20:38 plaintext.txt
-rw-r--r-- 1 morten morten 144 2010-09-08 20:38 plaintext.txt.gpg
morten@debian504intel:/tmp$ gpg < plaintext.txt.gpg
gpg: TWOFISH encrypted data
gpg: encrypted with 1 passphrase
Attack at Dawn.
morten@debian504intel:/tmp$

more about gpg on gnupg hacks,

please how do I operate your program?

I inspoect my encrypted file as binary in ghex2 and it is really encrypted.

I tried running your code, there are multiple issues with it but I'll just list the ones that jumped out to me and leave you to figure out the others.

[Line 5] Change your char *ref[52] to be char ref[52], and every other line in your code that uses *ref change to ref. Also for tidiness' sake, change those " to ' in your array.
[Line 10] void main() is not standard, at the very least it should be int main()
[Line 76] Just no, you shouldn't be adding to key like this, what I would do is this:

int place = 0;
int len = strlen(key); // get our string length

if(place >= len)
{
	place = 0; // get a place our the char array key
}
result = (i + key[place]) % 52;

Changing every line that uses ref should stop the output crashing, but I doubt it'll work. I would make the suggestion of reading this, it's a bit advanced but it gives you the theory behind AES, you don't need to apply everything in there but it gives you an idea of how encryption works. Act 3 is where the interesting stuff comes in, but I recommend reading up until it gets too difficult.

Before you dive into this though I think it'd pay you to learn a bit more C, specifically file handling and string manipulation because this is obviously where you are faltering.

Thanks for the all the great replies and suggestions, i will act on them as soon as i get the time;

@setherith: I have compiled it and run it and there r no errors...but the program doesn't quite work the way i want it to...

@morten42: The key should actually be a number and the source file should be present in the root folder of the program and at this point in time the program only accept alphabets, i will put up an updated code as soon as i get it running properly....

@Auraomega: thanks for your suggestions. the reason that i used *ref[52] instead of ref[52] is because my compiler (VB) was giving me an error saying "too many initializers", and i am not sure if you see it but the key is actually a number (int) and not a string as many people are assuming...and also on the topic of file handling...i was under the impression that i had got it pretty much covered (atleast for the bits that i uses before) so it would be great if you could tell me where i was faltering and i will put up a list of the exact problems i am having with this program very soon....

Visual Basic? Do you mean Visual C++? I ran it in VC++08 and Mingw and got the errors above, any compiler that follows the C99 standard will give warnings at the very least about using void main too.

Anyway, as I said you need to make ref ref[52] all the way along. I'm aware of how you've used key, hence why I explicitly said array and not string (however you're printing in the output file like a string), you still want to handle it as a char and not an int (having a signed char go into a signed int will cause you issues, trust me).

As for file handling, I made a mistake and assumed you were working with binary binary, however if you want to encrypt more than just plain text you will have to change the way you've opened you files as well as the methods you use to read them.

I'm going to leave you with the info I've given to try and fix it yourself, at the very least you should be getting different errors. Once you've changed everything over and tried to fix whatever else you get feel free to post your new code and I'll continue helping as and when I can.

This article has been dead for over six months. Start a new discussion instead.