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....