Sunday, 9 March 2014

How to make encoding and decoding program in C language

This a program which you can use to encode your program file or text file so that no one can see it and latter on you can also decode it by the same program.




 SOURCE CODE OUTPUT EXPLANATION DOWNLOAD


#include<stdio.h>
void main()
{
  FILE *fp1,*fp2;
  char sf[20],tf[20];
  int ch,select;
  clrscr();
  printf("\n1.)Encode File\t\t\tpress  1\n2.)Decode File\t\t\tpress  2");
  scanf("%d",&select);
  printf("\nEnter the name of source file with extension ");
  scanf("%s",sf);
  printf("\nEnter the name of target file with extension ");
  scanf("%s",tf);
  fp1=fopen(sf,"r");
  fp2=fopen(tf,"w");
  if(fp1==NULL||fp2==NULL)
  {
    printf("Unable to open the file ");
    getch();exit(1);
  }
  switch(select)
  {
    case 1:
while((ch=getc(fp1))!=EOF)
putc(ch+127,fp2);
printf("File has being encoded successfully");
break;
    case 2:
while((ch=getc(fp1))!=EOF)
putc(ch-127,fp2);
printf("File has being decoded successfully");
break;
    default:
printf("Sorry Wrong choice");
break;
   }
   getch();
   fclose(fp1);
   fclose(fp2);
}

First we will open the file which has to be encoded in read mode and then open the file in which we want to put our encoded program .Now through while loop we will read each characters of the file and add 127 to it and then save it in another file.Thus in this way our file or program gets encoded .Now when we want to decode it we will just subtract 127 from each character from the encoded file and then save it to another file .Thus our file has been decoded.You can also add or subtract different numbers I have subtract 127 because after 127 ASCII values of graphics characters starts.  
Any questions regarding to program please write in comments.

No comments:

Post a Comment