Home      Affiliated Colleges      Course content      First Sem     Second Sem     Third Sem     Fourth Sem     Fifth Sem     Sixth Sem     Seventh Sem     Eighth Sem     Lab report 4th sem     Contact    

Monday, August 9, 2010

Introduction TO CryproGraphy: C Program

a) Read a text file from a disk.
b) Store some english string in an array.
c) Take an alphabet as input i.e. key.
d) Find the position of alphabet in English alphabet set.
e) For each character in array, find new character which is after the no. of    position of the alphabet.
f) Create a file and write the new string to the file.
Program:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int readfile(char myarray[])
{
       FILE *fp;
       int i = 0;
       char c;
       fp=fopen("abc.txt","r");
       while(! feof(fp))
       {
                   c=fgetc(fp);
                   myarray[i]=c;
                   i++;

       }
       fclose(fp);
       cout<<myarray;
       getch();
       return i;

}
void main()
{
       FILE *ft;
       char myarray[50];
       char cipher[50];
       char key;
       int pos,n;
       clrscr();
       cout<<"The text form disk is :"<<endl;
       n=readfile(myarray);
       cout<<"\nEnter the encryption  key :";
       cin>>key;
       pos =tolower(key)-96;

       for(int i =0;i<n;i++)
       {

                    if(myarray[i]>='a' && myarray[i]<='z')
                   {
                               cipher[i]=myarray[i]+pos;
                               if(cipher[i]>122 )
                                           cipher[i]=cipher[i]-26;
                   }
                   else if(myarray[i]>='A' && myarray[i]<='Z')
                   {
                               cipher[i]=myarray[i]+pos;
                               if(cipher[i]>90)
                                           cipher[i]=cipher[i]-26;
                   }
                   else
                               cipher[i]=myarray[i];

       }
       cout<<cipher;
       ft=fopen("xyz.txt","w");
       for(i=0;i<n;i++)
       {

                   putc(cipher[i],ft);
       }
       getch();
}

No comments:

Post a Comment

^ Scroll to Top Related Posts with Thumbnails ^ Go to Top