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    

Wednesday, January 26, 2011

Introduction to Cryptography: C Program for Caesar Cipher

//Caesar cipher
#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 from 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