Computer Graphics Program source codes with full description.

Study of Various C Graphics Functions

Implementation and Using mouse in DOS

Implementation of DDA line algorithm with source code in C/C++.

Implementation of Bresenham Line algorithm with source code in C/C++.

Implementation of Midpoint Line algorithm with source code in C/C++.

Implementation of Bresenham Circle algorithm with source code in C/C++.

Implementation of Mid-point Circle algorithm with source code in C/C++.

Implementation of Mid-point Ellipse algorithm with source code in C/C++.

Implementation of Polygon Filling using Scan Fill with source code in C/C++.

Implementation of Polygon Filling using Flood Fill with source code in C/C++.

Implementation of Polygon Filling using Boundary Fill Algorithms.

Implementation of algorithm of 2D Transformation of an Object with source code in C/C++.

Implementation of Line Clipping using Cohen- Sutherland algorithm with source code in C/C++.

Implementation of Line Clipping using Liang-Barky algorithm with source code in C/C++.

Implementation of Polygon clipping using Sutherland-hodgeman algorithm with source code in C/C++.

Search

Friday 24 June 2011

Playfair cipher encryption-decryption

To implement Playfair cipher encryption-decryption you should see the following c code...



#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5
void playfair(char ch1,char ch2, char key[MX][MX])
{
 int i,j,w,x,y,z;
 FILE *out;
 if((out=fopen("cipher.txt","a+"))==NULL)
  {
   printf("File Currupted.");
  }

 for(i=0;i<MX;i++)
 {
  for(j=0;j<MX;j++)
  {
   if(ch1==key[i][j])
   {
    w=i;
    x=j;
   }
   else if(ch2==key[i][j])
   {
    y=i;
    z=j;
   }
  }
 }
 //printf("%d%d %d%d",w,x,y,z);
 if(w==y)
 {
  x=(x+1)%5;z=(z+1)%5;
  printf("%c%c",key[w][x],key[y][z]);
  fprintf(out, "%c%c",key[w][x],key[y][z]);
 }
 else if(x==z)
 {
  w=(w+1)%5;y=(y+1)%5;
  printf("%c%c",key[w][x],key[y][z]);
  fprintf(out, "%c%c",key[w][x],key[y][z]);
 }
 else
 {
  printf("%c%c",key[w][z],key[y][x]);
  fprintf(out, "%c%c",key[w][z],key[y][x]);
  }
  fclose(out);
}
void main()
 {

  int i,j,k=0,l,m=0,n;
  char key[MX][MX],keyminus[25],keystr[10],str[25]={0};
char
alpa[26]={'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'};
  clrscr();
  printf("\nEnter key:");
   gets(keystr);
  printf("\nEnter the plain text:");
    gets(str);
  n=strlen(keystr);
  //convert the characters to uppertext
  for (i=0; i<n; i++)
   {
    if(keystr[i]=='j')keystr[i]='i';
    else if(keystr[i]=='J')keystr[i]='I';
    keystr[i] = toupper(keystr[i]);

   }

  //convert all the characters of plaintext  to uppertext
  for (i=0; i<strlen(str); i++)
   {
    if(str[i]=='j')str[i]='i';
    else if(str[i]=='J')str[i]='I';
    str[i] = toupper(str[i]);

   }


  // store all characters except key
  j=0;
  for(i=0;i<26;i++)
  {
   for(k=0;k<n;k++)
   {
    if(keystr[k]==alpa[i])
    break;
    else if(alpa[i]=='J')
    break;
   }
   if(k==n)
   {
    keyminus[j]=alpa[i];j++;
   }
  }

  //construct key keymatrix
  k=0;
  for(i=0;i<MX;i++)
  {
   for(j=0;j<MX;j++)
    {
     if(k<n)
     {
     key[i][j]=keystr[k];
     k++;}
     else
     {
      key[i][j]=keyminus[m];m++;
      }
      printf("%c  ",key[i][j]);
    }
    printf("\n");
  }
 
  // construct diagram and convert to cipher text
  printf("\n\nEntered text :%s\nCipher Text :",str);
  for(i=0;i<strlen(str);i++)
  {
   if(str[i]=='J')str[i]='I';
   if(str[i+1]=='\0')
    playfair(str[i],'X',key);
   else
   {
    if(str[i+1]=='J')str[i+1]='I';
    if(str[i]==str[i+1])
     playfair(str[i],'X',key);
    else
    {
     playfair(str[i],str[i+1],key);i++;
    }
   }
  }
  getch();
 }

output:




4 comments:

  1. sorry for interrupting u..can give me more explanation for the coding above

    ReplyDelete
  2. The code is completely incorrect. If letters repeat in the key then you have to ignore them and fill the remaining letters in sequence. Thus the matrix this program generates is completely incorrect.

    ReplyDelete
  3. I can't find the decryption program

    ReplyDelete

Copyright Text

Copyright @ LDRP Student Community, webmaster Rahul Bhadauriya