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

Rail-Fence cipher encryption-decryption

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()

{
            int i,j=0,d,k=0;
            char p[50],ct[50][50];
            clrscr();
            printf("Enter the plain text:\n");
            gets(p);
            printf("\nEnter the depth in the integer:");
            scanf("%d",&d);
            //declare null for empty array values
            for(i=0;i<50;i++)
            {
                        for(j=0;j<50;j++)
                        {
                                    ct[i][j]='\0';
                        }
            }
            k=0;
       //loop up to string lenght of the pliantext
            {
            for(i=0;i<strlen(p);i++)
            {
                        for(j=0;j<d;j++)
                        {
                                    if(k<=strlen(p))
                               ct[i][j]=p[k];
                                    k++;
                        }
                        ct[i][j]='\0';


                        }
            }

            for(i=0;i<d;i++)
            {
                        for(j=0;j<strlen(p);j++)
                        {

                                    if(ct[j][i]!='\0')
                                       {
                                           printf("%c",ct[j][i]);
                                       }
                        }

                        printf("\n");
            }
            // Read the text
            printf("\nThe encrypted text is:\n");
            for(i=0;i<d;i++)
            {
                        for(j=0;j<strlen(p);j++)
                        {
                                    if(ct[j][i]!='\0')
                                    printf("%c",ct[j][i]);
                        }
            }

            getch();
}


output:

columnar transposition cipher encryption-decryption.

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
            char k[20]={'\0'},pt[30]={'\0'},key[30]={'\0'},mat[10][10]={'\0'},ct[30]={'\0'};
            int i=0,j=0,no[10]={0},ctr,z,c=0,a=0;
            clrscr();
            printf("Enter the plaintext:\n");
            gets(pt);
            printf("Enter the key:\n");
            gets(k);
            strcpy(key,k);
            strcat(k,pt);
            strcpy(pt,k);
            printf("Enter the ordering starting from 0 to the (keylength-1):\n");

            for(i=0;i<strlen(key);i++)
            {

                        scanf("%d",&no[i]);
            }
            j=0;
            ctr=j;
            printf("The matrix is:\n");
            for(i=0;i<strlen(pt);i++)
            {
                        for(j=j;ctr<strlen(key);j++)
                        {
                                    if(pt[j]!='\0')
                                    {
                                                mat[i][ctr]=pt[j];
                                                printf("%c",mat[i][ctr]);
                                                ctr++;
                                    }
                                    else
                                    {
                                                goto out;
                                    }
                        }
                        ctr=0;
                        if(i==0) printf("\n");
                        printf("\n");
            }
            out:
            printf("\n\nThe Encrypted  text is:\n");
            for(i=0;i<strlen(key);i++)
            {
                        for(j=0;j<strlen(key);j++)
                        {
                                    if(no[i]==j)
                                    {
                                           for(z=1;z<10;z++)
                                           {
                                                            if(isalpha(mat[z][j]))
                                                            {
                                                            printf("%c",mat[z][j]);
                                                            ct[a]=mat[z][j];
                                                            a++;
                                                            }

                                           }
                                    }
                        }
            }
            printf("\n\nDecrypted Ciphertext is:\n");

            for(a=0;a<30;a++)
            {
            if(isalpha(ct[a]) || isspace(ct[a]))
            c++;
            }

            for(i=1;i<c;i++)
            {
                        for(j=0;j<strlen(key);j++)
                        {
                                    if(isalpha(mat[i][j]))
                                    printf("%c",mat[i][j]);
                                    else
                                    goto ext;
                        }
            }
            ext:

            getch();
}


output:



Polyalphabetic cipher encryption-decryption

the source code for Polyalphabetic cipher encryption-decryption is as follows in c..



#include<stdio.h>
#include<conio.h>
#include<string.h>


char pt[40]={'\0'},key[40]={'\0'},ct[40]={'\0'},pta[40]={'\0'},k[40]={'\0'};
int i,j;      // global values

void main()
{
            clrscr();
            printf("\nEnter the keyword:\n");
            gets(k);//read the key
            printf("\nEnter the Plain text:\n");
            gets(pt);    //read the plain text

            // print the table
            printf("The convergen matrix\n");
            printf("\n   ");
            for(j=97;j<=122;j++)
            {
                        printf(" %c",j);
            }

            printf("\n--------------------------------------------------------\n");
            for(i=97;i<=122;i++)
            {
                        printf("%c |",i);
                        for(j=97;j<=122;j++)
                        {

                                    if(((i+j))>219)
                                    {

                                                printf(" %c",toupper((i+j)-123));
                                    }
                                    else
                                    {
                                                printf(" %c",toupper((i+j)-97));
                                    }
                        }
                        printf("\n");
            }
            // for keyword
            j=0;
            for(i=0;i<strlen(pt);i++)
            {
                        key[i]=k[j];
                        if(j==(strlen(k)-1))
                        {
                                    j=0;
                        }
                        else
                        {
                                    j++;
                        }
            }
            for(i=0;i<(strlen(pt)-1);i++);
            k[i]='\0';
            printf("\nThe encrypted text is:\n");
            // encryption
            for(i=0;i<strlen(pt);i++)
            {
                        if(97<=(int)pt[i] && (int)pt[i]<=122)
                        {

                                    if(((int)pt[i]+(int)key[i])>219)
                                    {
                                                ct[i]=(int)pt[i]+(int)key[i]-123;
                                    }
                                    else
                                    {
                                                ct[i]=(int)pt[i]+(int)key[i]-97;
                                    }
                                    printf("%c",toupper(ct[i]));
                        }
                        else
                        {
                                    ct[i]=pt[i];
                                    printf("%c",pt[i]);
                        }
            }
            // decryption
            printf("\n\nDecrypted text is:\n");
            for(i=0;i<strlen(ct);i++)
            {
                        if(97<=(int)ct[i] && (int)ct[i]<=122)
                        {

                                    if(((int)ct[i]-(int)key[i])<0)
                                    {
                                                pta[i]=((int)ct[i]-(int)key[i])+123;
                                    }
                                    else
                                    {
                                                pta[i]=(int)ct[i]-(int)key[i]+97;
                                    }
                                    printf("%c",pta[i]);
                        }
                        else
                        {
                                    pta[i]=ct[i];
                                    printf("%c",pta[i]);
                        }
            }
            getch();
}


output:




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:




Copyright Text

Copyright @ LDRP Student Community, webmaster Rahul Bhadauriya