Pages

IMPLEMENT CAESAR CIPHER WITH VARIABLE KEY

It is an encryption technique in which each plaintext letter is to be replaced with one a fixed number of
places (in following implementation, key) down the alphabet. This example is with a shift of three
(key=3), so that a B in the plaintext becomes E in the ciphertext.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main()
    {
    clrscr();
    char message[80],ciphertext[80],decryptedtext[80];
    int i;

    printf("Enter original message : ");
    scanf("%[^\n]",message);

    for(i=0; i<strlen(message); ++i)
        {
        if(isalpha(message[i]))
            {
            if((message[i]+3)>'z')ciphertext[i]='a'+message[i]+3-'z'-1;
            else ciphertext[i]=message[i]+3;
            }
        else
            {
            ciphertext[i]=message[i];
            }
        }
    ciphertext[i]='\0';
    printf("Cipher text            : %s\n",ciphertext);

    getch();
    }

IMPLEMENT 3 X 3 HILL CIPHER

Hill cipher is a polygraphic substitution cipher based on linear algebra. Each letter is first encoded as a
number. Often the simplest scheme is used: A = 0, B =1, ..., Z=25. A block of n letters is then considered
as a vector of n dimensions, and multiplied by a n × n matrix, modulo 26.
Encryption:
For Example, the key is given by the 3 x 3 matrix and the message="ACT".
Since 'A' is 0, 'C' is 2 and 'T' is 19, the message is the vector:

 

which corresponds to a ciphertext of 'POH'.
Decryption:
In order to decrypt, we turn the ciphertext back into a vector, then simply multiply by the inverse matrix
of the key matrix.
  

which gets us back to 'ACT'.


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char aa[26]="abcdefghijklmnopqrstuvwxyz";
char pt[10];
int m,d,q=0,i,j,k[2][2],p[4],pp[4],t[5];
int k1[2][2],k2[2][2],det;
clrscr();
printf("enter the plaintext:" );
scanf("%s",pt);
m=strlen(pt);
printf("enter the numbers:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&k[i][j]);
}
}

for(i=0;i<m;i++)
{
for(j=i;j<26;j++)

{
if(pt[i]==aa[j])

{
t[q]=j%26;
++q;
}
}
}
p[0]=(k[0][0]*t[0])+(k[0][1]*t[1]);
p[1]=(k[1][0]*t[0])+(k[1][1]*t[1]);
p[2]=(k[0][0]*t[2])+(k[0][1]*t[3]);
p[3]=(k[1][0]*t[2])+(k[1][1]*t[3]);
k1[0][0]=k[1][1];
k1[0][1]=-(k[0][1]);
k1[1][0]=-(k[1][0]);
k1[1][1]=k[0][0];

det=(k1[0][0]*k1[1][1])-(k1[0][1]*k1[1][0]);
for(i=0;i<26;i++)
{
if((det*i)%26==1)
{
d=i;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
k2[i][j]=(d*k1[i][j])%26;


}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(k2[i][j]<0)
k2[i][j]+=26;
}
}
pp[0]=((k2[0][0]*p[0])+(k2[0][1]*p[1]))%26;
pp[1]=((k2[1][0]*p[0])+(k2[1][1]*p[1]))%26;
pp[2]=((k2[0][0]*p[2])+(k2[0][1]*p[3]))%26;
pp[3]=((k2[1][0]*p[2])+(k2[1][1]*p[3]))%26;
for(i=0;i<m;i++)
{
printf("\nThe decrypted plain text :%c",aa[pp[i]]);
}
getch();
}

IMPLEMENT RAIL FENCE CIPHER

Algorithm:
The Rail Fence cipher is a form of transposition cipher that gets its name from the way in which it is
encoded. In the rail fence cipher, the plaintext is written downwards on successive "rails" of an imaginary
fence, then moving up when we get to the bottom. The message is then read off in rows. For example, using
three "rails" (level=3) and a message of 'SKY IS THE LIMIT’, encipherment would be:
S  Y  S  H  L  M  T
  K  I  T   E  I    I
And would be read as (One rail at a time from top to bottom): SYSHLMTKITEII

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char cipher[10][100],encrypted[100],decrypted[100],plaintext[100]="skyisthelimit" ;
int i,j,level,k=0,l=0;
clrscr();
printf("enter level:");
scanf("%d",&level);

      for(i=0;i<level;i++)
      {
       for(j=0;j<strlen(plaintext);j++)
       {
           cipher[i][j]=' ';
       }
      }
      printf("%d %d",cipher[i][j]);
       for(i=0;i<strlen(plaintext);i++)
       {
       cipher[k%level][l++]=plaintext[i];
       k++;
       }
       for(i=0;i<level;i++)
       {
        cipher[i][l]='\0';
       }
       for(i=0;i<level;i++)
       {
      printf("row %d=%s\n",i,cipher[i]);
       }
       k=0;
       for(i=0;i<level;i++)
       {
      for(j=i;j<strlen(plaintext);j+=level)
      {
          encrypted[k++]=cipher[i][j];
      }
       }
       encrypted[k]='\0';
       printf("encrypted text=%s\n",encrypted);
          k=l=0;
          for(i=0;i<=strlen(plaintext)/level;i++)
          {
          for(j=0;j<level;j++)
          {
              decrypted[l++]=cipher[j][k++];
          }
          }
          decrypted[k]='\0';
          printf("decrypted text=%s",decrypted);

       getch();
}

IMPLEMENT SIMPLE TRANSPOSITION

Example:
Message: welcome
Key: 641352
Encrypted Text: lecemw
Decrypted Text: welcome


#include <stdio.h>
#include <conio.h>
#include <string.h>
#define block_size 5
void main()
    {
    char plaintext[80]="HELLOVIREN",ciphertext[80],decryptedtext[80];
    int key[5]={4,2,0,1,3};
    int len,i,j;
    clrscr();
    len=strlen(plaintext);
    for(i=0; i<len; i+=5)
        {
        for(j=i; j<block_size+i; ++j)
            {
            ciphertext[j]=plaintext[i+key[j-i]];
            }
        }
    ciphertext[i]='\0';
    printf("\n\n\n");
    printf("Plain Text  = %s\n",plaintext);
    printf("Cipher Text = %s\n",ciphertext);
    getch();
    }

Boundry fill & Flood fill algo

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundary(int,int,int,int);
void flood_fill(int,int,int,int);
void main()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"c:\\tc\\bgi");
    rectangle(50,50,100,100);
    boundary(55,55,4,15);
    flood_fill(55,55,4,1);
    getch();
    closegraph();

}
void boundary(int seed_x,int seed_y,int f_col,int b_col)
{
    if(getpixel(seed_x,seed_y)!=b_col && getpixel(seed_x,seed_y)!=f_col)
    {
        putpixel(seed_x,seed_y,f_col);
        boundary(seed_x+1,seed_y,f_col,b_col);
        boundary(seed_x-1,seed_y,f_col,b_col);
        boundary(seed_x,seed_y+1,f_col,b_col);
        boundary(seed_x,seed_y-1,f_col,b_col);
        boundary(seed_x+1,seed_y+1,f_col,b_col);
        boundary(seed_x-1,seed_y-1,f_col,b_col);
        boundary(seed_x+1,seed_y-1,f_col,b_col);
        boundary(seed_x-1,seed_y+1,f_col,b_col);
    }

}
void flood_fill(int seed_x,int seed_y,int old_col,int new_col)
{
    if(getpixel(seed_x,seed_y)==old_col)
    {
        putpixel(seed_x,seed_y,new_col);
        flood_fill(seed_x+1,seed_y,old_col,new_col);
        flood_fill(seed_x-1,seed_y,old_col,new_col);
        flood_fill(seed_x,seed_y+1,old_col,new_col);
        flood_fill(seed_x,seed_y-1,old_col,new_col);
        flood_fill(seed_x+1,seed_y+1,old_col,new_col);
        flood_fill(seed_x-1,seed_y-1,old_col,new_col);
        flood_fill(seed_x+1,seed_y-1,old_col,new_col);
        flood_fill(seed_x-1,seed_y+1,old_col,new_col);

    }

}

Draw Ellipse

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#define ROUND(a) ((int) (a+0.5))
void main()
{
int gd=DETECT,gm,errorcode,round(float);
float p,x,y,xc,yc,rx,ry,ry2,rx2;
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("Enter the value for xc,yc,rx and ry\n");
scanf("%f %f %f %f",&xc,&yc,&rx,&ry);

ry2=ry*ry;

rx2=rx*rx;
//region 1

x=0;
y=ry;
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
p=ROUND((ry2-rx2*ry+(0.25*rx2)));

while((2*ry2*x)<(2*rx2*y))
{

if(p<0)
{
x++;
p=p+(2*ry2*x)+(2*ry2)+ry2;

}
else
{
x++;
y--;
p=p+(2*ry2*x)+(2*ry2)-(2*rx2*y)-(2*rx2)+(ry2);
}
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
}

//region2

p=ROUND(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{


if(p<0)
{
x++;
y--;
p=p+(2*ry2*x)+(2*ry2)-(2*rx2*y)-(2*rx2)+(rx2);
}
else
{
y--;
p=p-(2*rx2*y)-(2*rx2)+rx2;
}
//putpixel(xc,yc,6);
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
}
outtextxy(xc,yc,"(xc,yc)");
getch();
}

Draw Circle using midpoint algorthem


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
//void oncircle(int,int);
void main()
{
//clrscr();
int gd=DETECT,gm,xc,yc,r;
int  x0,y0,d,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("please enter the center point co-ordinates:-");
scanf("%d %d",&xc,&yc);
printf("please enter the radius:-");
scanf("%d",&r);
x=0;
y=r;
d=(5/4)-r;
//putpixel(x0,y0,1);
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);

while(y>x)
{
if(d<0)
{
d=d+2*(x)+3;
x=x+1;
y=y;
}
else
{
d=d+2*(x-y)+5;
x=x+1;
y=y-1;
}

putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}
getch();
closegraph();
}

Translation,Rotation,Scallig

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
# define pi 3.14
void translation(int[]);
void rotation(int[]);
void scalling(int[]);
int vr;
void main()
    {
    int gmode,gdrive=DETECT;
    int i,poly[10],ch;
    clrscr();
    initgraph(&gdrive,&gmode,"C:\\TC\\BGI");
    printf("ENTER NUMBER  OF POLYGON VECTOR:");
    scanf("%d",&vr);
    for(i=0;i<2*vr;i+=2)
        {
        printf("ENTER VECTOR %d :",i/2+1);
        scanf("%d %d",&poly[i],&poly[i+1]);
        }
    poly[i] = poly[0];
    poly[i+1] = poly[1];
    drawpoly(vr+1,poly);
    while(1)
    {
    printf("1.TRANSLATION:\n");
    printf("2.ROTATION:\n");
    printf("3.SCALLING:\n");
    printf("4.EXIT:\n");
    printf("ENTER YOUR CHOICE:");
    scanf("%d",&ch);
    switch(ch)
        {
        case 1:
            translation(poly);
            break;
        case 2:
            rotation(poly);
            break;
        case 3:
            scalling(poly);
            break;
        case 4:
            exit(0);
        default:
            printf("INVALID CHOICE......\n");
        }
    }
    getch( );
    closegraph();
    }
void translation(int poly[])
    {
    int i,tx,ty;
    printf("ENTER TRANSLATION VECTOR:");
    scanf("%d %d",&tx,&ty);
    for(i=0;i<2*vr;i+=2)
        {
        poly[i]+=tx;
        poly[i+1]+=ty;
        }
    poly[i] = poly[0];
    poly[i+1] = poly[1];
    drawpoly(vr+1,poly);
    }
void rotation(int poly[])
    {
    int i,thita,t1,t2;
    printf("ENTER ROTATION ANGLE:");
    scanf("%d",&thita);
    for(i=0;i<2*vr;i+=2)
        {
        t1=poly[i]*cos(thita*pi/180)-poly[i+1]*sin(thita*pi/180);
        t2=poly[i]*sin(thita*pi/180)+poly[i+1]*cos(thita*pi/180);
        poly[i]=t1;
        poly[i+1]=t2;
        }
    poly[i] = poly[0];
    poly[i+1] = poly[1];
    drawpoly(vr+1,poly);
    }
void scalling(int poly[])
    {
    float sx,sy;
    int i,xf,yf;
    printf("ENTER SCALLING CORDINATE");
    scanf("%d %d",&xf,&yf);
    printf("ENTER SCALLING VECTOR:");
    scanf("%f %f",&sx,&sy);
    for(i=0;i<2*vr;i+=2)
        {
        poly[i]=poly[i]*sx+xf*(1-sx);
        poly[i+1]=poly[i+1]*sy+yf*(1-sy);
        }
    poly[i] = poly[0];
    poly[i+1] = poly[1];
    drawpoly(vr+1,poly);
    }

DDA line drawing algo for stylist lines


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void main()
{
int gd=DETECT,gm,ch,k,w,j,q;
int x1,x2,y1,y2;
float xiner,yiner,x,y;
float dx,dy,steps,i,l;
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("ENTER THE VALUE OF xA,XB,YA,YB   \n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;

if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);

xiner=dx/steps;
yiner=dy/steps;
do
{
x=x1;
y=y1;
putpixel(x,y,6);

printf("\nenter the choice for line type");
printf("\n 1. solid line");
printf("\n 2. dash line");
printf("\n 3. dot line");
printf("\n 4.dashdot line");
printf("\n5.thick line");
scanf("%d",&ch);
cleardevice();
switch(ch)
{

case 1:  //solid
    for(k=1;k<=steps;k++)
    {
    putpixel(x,y,4);
    x=x+xiner;
    y=y+yiner;
    outtextxy(x2,y2,"(xb,yb)");
    outtextxy(x1,y1,"(xa,ya)");

      }
    break;

case 2:         //dashed
    for(k=1;k<=steps;k++)
    {
     if( (k%5!=0) )
     putpixel(x,y,3);
     x=x+xiner;
     y=y+yiner;
    outtextxy(x2,y2,"(xb,yb)");
    outtextxy(x1,y1,"(xa,ya)");

    }
     break;

case 3:                 //dot
    for(k=1;k<=steps;k++)
    {
     if( (k%2!=0) )
     putpixel(x,y,3);
     x=x+xiner;
     y=y+yiner;
    outtextxy(x2,y2,"(xb,yb)");
    outtextxy(x1,y1,"(xa,ya)");

    }
     break;

case 4:
                 //dashdot
    for(k=1;k<=steps;k++)
    {
    if(k%8!=0)
    {
    putpixel(x,y,3);
    }
    else
    {
     x=x+xiner;
     y=y+yiner;
    k++;
    putpixel(x,y,4);
    x=x+xiner;
    y=y+yiner;
    k++;
    }
    x=x+xiner;
    y=y+yiner;
    outtextxy(x2,y2,"(xb,yb)");
    outtextxy(x1,y1,"(xa,ya)");

       }
     break;




 case 5:      //thick
    printf("\n enter width:");
    scanf("%d",&w);
    for(k=1;k<=steps;k++)
    {
    if(x1==x2)
    {              for(j=1;j<=w;j++)
            {
            putpixel(x+j,y,1);
            }

    }
    else if(y1==y2)
    {
    for(j=1;j<=w;j++)
            {
            putpixel(x,y+j,1);
            }

    }
    else
    {
    for(j=1;j<=w;j++)
            {
            putpixel(x+j,y-j,1);
              }


    }
    x=x+xiner;
    y=y+yiner;

    outtextxy(x1,y1,"(xb,yb)");
    outtextxy(x2,y2,"(xa,ya)");
    }
     break;

 }
printf("\nDO U WANT TO CONTINUE?(1/0)");
scanf("\n%d",&q);
}while(q!=0);
getch();
}