Mar 18, 2010

Midpoint ellipse algorithm

//This is Midpoint ellipse algorithm developed using C language

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void jayuellipse(int x,int y,int rx,int ry);
void print(int,int,int,int);
void main()
{
int xc,yc,p,r,rx,ry,gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\bgi");
printf("enter values of (x,y,xrad,yrad)"),scanf("%d%d%d%d",&xc,&yc,&rx,&ry);
jayuellipse(xc,yc,rx,ry);
getch();
}
void jayuellipse(int xc,int yc,int rx,int ry)
{
long int p,p2,x,y,dx,dy,ry2,rx2,rx2ry,hfrx2,_2ry2x,_2rx2y;
float x5;
//initialization of values
x=0;
y=ry;
ry2=ry*ry;
rx2=rx*rx;
rx2ry=rx*rx*ry;
hfrx2=.25*rx*rx;
p=ry2-rx2ry+hfrx2;
_2ry2x=2*ry*ry*x;
_2rx2y=2*rx*rx*y;
dx=_2ry2x;
dy=_2rx2y;
//calculation for region 1
while(dx<dy)
{
print(x,y,xc,yc);
if(p<0)
{
x++;
dx+=2*ry2;
p+=dx+ry2;
}
else if(p>=0)
{
x++;
y--;
dx+=2*ry2;
dy-=2*rx2;
p+=dx+ry2-dy;
}
}
//end of region 1
 x5=x+.5;
p2=(ry2*x5*x5)+(rx2*(y-1)*(y-1))-(rx2*ry2);
//calculation for region 2
while(y>=0)
{
print(x,y,xc,yc);
if(p2>0)
{
y--;
dx+=-(2*rx2);
p2+=rx2-(dx);
}
else
{
x++;y--;
dx+=-(2*rx2);
dy+=2*ry2;
p2+=rx2-(dx)+(dy);
}
}
}
void print(int x,int y,int xc,int yc)
{
putpixel(x+xc,y+yc,7);
putpixel(x+xc,-y+yc,7);
putpixel(-x+xc,y+yc,7);
putpixel(-x+xc,-y+yc,7);
}

Mar 10, 2010

Playfair cipher encyption and decryption

//PLAYFAIR CIPHER ENCRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
int i,m=0,flag=0,j,n,row1,row2,col1,col2;
char ch,d,mat[5][5],key[10],k[10],c[30],a[10],ct[10],pt[10];
clrscr();
fp=fopen("key.txt","r");
fscanf(fp,"%s",key);
fclose(fp);
printf("The key is: %s",key);
//finding new key removing duplicates...
for(i=0;i<strlen(key);)
{
if(key[i]=='j')
key[i]=105;
flag=0;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
flag=1;
}
if(flag==1)
i++;
else
k[m++]=key[i++];  //new array for key
}
k[m]=NULL;
printf("\nThe new key is  :\n%s",k);//printing new key
//store key in matrix
m=0;
for(ch=97;ch<=122;)
{
flag=0;
for(i=0;i<strlen(k);i++)
{
if(k[i]==ch)
flag=1;
}
if(flag==1)
ch++;
else if(ch=='j')
ch++;
else
{
c[m++]=ch;
ch++;
}
}
c[m]=NULL;
//construction of 2-D 5*5 matrix
m=0;
n=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
mat[i][j]=k[m++];
if(m>strlen(k))
mat[i][j]=c[n++];
}
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
printf("\t %c",mat[i][j]);
printf("\n");
}
//retrieve plain-text from file
i=0;
fp=fopen("plain.txt","r");
fscanf(fp,"%s",pt);
fclose(fp);
printf("\nThe plain-text is: %s",pt);
//seperation of digram
j=0;
printf("\nAfter Processing Digrams: ");
for(i=0;i<strlen(pt);)
{        if(pt[i]==pt[i+1])
{
a[j++]=pt[i];
a[j++]='x';
i+=1;
}
else
{
a[j++]=pt[i];
a[j++]=pt[i+1];
i+=2;
}
}
a[j]='\0';
printf("%s",a);
printf("\n");
for(i=0;i<strlen(a);i+=2)//Loop for conversion to cipher text
{
for(j=0;j<5;j++)// matrix row
{
for(m=0;m<5;m++)//matrix column
{
//Find position of 1st element of Digram in matrix
if(a[i]==mat[j][m])
{
row1=j;
col1=m;
}
//Find position of 2nd element of Digram in matrix
if(a[i+1]==mat[j][m])
{
row2=j;
col2=m;
}
//Condition For both elements in same row of matrix
if(row1==row2)
{
ct[i]=mat[row1][(col1+1)%5];
ct[i+1]=mat[row1][(col2+1)%5];
}
//Condition For both elements in same column of matrix
else if(col1==col2)
{
ct[i]=mat[(row1+1)%5][col1];
ct[i+1]=mat[(row2+1)%5][col2];
}
else
{
ct[i]=mat[row1][col2];
ct[i+1]=mat[row2][col1];
}
}
}
}
ct[i]='\0';
fp=fopen("cipher.txt","w");
printf("\nCipher Text: ");
//store cipher text in a file
for(i=0;i<strlen(ct);i++)
putc(ct[i],fp);
fclose(fp);
printf("%s",ct);      //print cipher text
getch();
}


//PLAYFAIR CIPHER DECRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
int i,m=0,flag=0,j,n,row1,row2,col1,col2;
char ch,d,mat[5][5],key[10],k[10],c[30],a[10],ct[10],pt[10];
clrscr();
fp=fopen("key.txt","r");
fscanf(fp,"%s",key);
fclose(fp);
printf("The key is: %s",key);
for(i=0;i<strlen(key);)
{
if(key[i]=='j')
{
key[i]='i';
}
flag=0;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
flag=1;
}

if(flag==1)
i++;
else
k[m++]=key[i++];  //new array for key
}
k[m]=NULL;
// printing new key after elimination of duplicates
printf("\nThe new key is  :\n%s",k);
//seperation of key letters from a-z & later stored in an array
m=0;
for(ch=97;ch<=122;)
{
flag=0;
for(i=0;i<strlen(k);i++)
{
if(k[i]==ch)
flag=1;
}
if(flag==1)
ch++;
else if(ch=='j')
ch++;
else
{
c[m++]=ch;
ch++;
}
}
c[m]=NULL;
//construction of 2-D 5*5 matrix
m=0;
n=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
mat[i][j]=k[m++];
if(m>strlen(k))
mat[i][j]=c[n++];
}
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("\t %c",mat[i][j]);
}
printf("\n");
}

//retrieve cipher-text from file

i=0;
fp=fopen("cipher.txt","r");
fscanf(fp,"%s",ct);
fclose(fp);
printf("\nThe Cipher-text is: %s",ct);



printf("\n");
for(i=0;i<strlen(ct);i+=2)//conversion to plain text
{
for(j=0;j<5;j++) //matrix row
{
for(m=0;m<5;m++)//matrix column
{
//Find position of 1st element of Digram in matrix
if(ct[i]==mat[j][m])
{
row1=j;
col1=m;
}
//Find position of 2nd element of Digram in matrix
if(ct[i+1]==mat[j][m])
{
row2=j;
col2=m;
}
//Condition For both elements in same row of matrix
if(row1==row2)
{
pt[i]=mat[row1][(col1-1+5)%5];
pt[i+1]=mat[row1][(col2-1+5)%5];
}
//Condition For both elements in same column of matrix
else if(col1==col2)
{
pt[i]=mat[(row1-1+5)%5][col1];
pt[i+1]=mat[(row2-1+5)%5][col2];
}
else
{
pt[i]=mat[row1][col2];
pt[i+1]=mat[row2][col1];
}
}
}
}
pt[i]='\0';
fp=fopen("plain.txt","w");
printf("\nPlain Text: ");
//store plain text in a file
for(i=0;i<strlen(pt);i++)
  putc(pt[i],fp);
fclose(fp);
printf("%s",pt);      //print plain text
fclose(fp);
getch();
}

Synchronous TDM and Asynchronous TDM

// This is  Synchronous TDM developed using C
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,k=0,l,maxlength=0,length[3],flag1=0,flag2=0,flag3=0;
char usr1[10],usr2[10],usr3[10],newusr1[10],newusr2[10],newusr3[10],b[100];
printf("enter data for user 1\n");
gets(usr1);
printf("enter data for user 2\n");
gets(usr2);
printf("enter data for user 3\n");
gets(usr3);
length[0]=strlen(usr1);
length[1]=strlen(usr2);
length[2]=strlen(usr3);
for(j=0;j<3;j++)
{
if(maxlength<length[j])
maxlength=length[j];
}
for(i=0;i<maxlength;i++)
{
if(usr1[i]!=NULL && flag1==0)
b[k++]=usr1[i];
else
{
b[k++]='$';
flag1=1;
}
if(usr2[i]!=NULL && flag2==0)
b[k++]=usr2[i];
else
{
b[k++]='$';
flag2=1;
}
if(usr3[i]!=NULL && flag3==0)
b[k++]=usr3[i];
else
{
b[k++]='$';
flag3=1;
}
}
b[k]=NULL;
k=0;
for(i=1;i<=maxlength;i++)
{
printf("\nframe %d is ",i);
for(j=0;j<3;j++)
printf("%c",b[k++]);
}
strcpy(newusr1,usr3);
strcpy(newusr2,usr1);
strcpy(newusr3,usr2);
printf("\nTransferring Data To Respective Users...\n\n");
printf("user1 gets %s \n",newusr1);
printf("user2 gets %s \n",newusr2);
printf("user3 gets %s \n",newusr3);
return 0;


}


//This is  Asynchronous TDM developed using C

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
int i,j,k=0,l,frame,maxlength=0,length_t=0,length[3],flag1=0,flag2=0,flag3=0;
char usr1[10],usr2[10],usr3[10],newusr1[10],newusr2[10],newusr3[10],b[100];
printf("enter data for user 1\n");
gets(usr1);
printf("enter data for user 2\n");
gets(usr2);
printf("enter data for user 3\n");
gets(usr3);
length[0]=strlen(usr1);
length[1]=strlen(usr2);
length[2]=strlen(usr3);
for(j=0;j<3;j++)
{
length_t+=length[j];
if(maxlength<length[j])
maxlength=length[j];
}

if(length_t%2==0)
frame=length_t/2;
else
frame=(length_t/2)+1;
printf("\ntotal frames are %d\n",frame);

for(i=0;i<maxlength;i++)
{
if(isalpha(usr1[i]))
b[k++]=usr1[i];
if(isalpha(usr2[i]))
b[k++]=usr2[i];
if(isalpha(usr3[i]))
b[k++]=usr3[i];
}
b[k]=NULL;
k=0;
for(i=1;i<=frame;i++)
{
printf("\nframe %d is ",i);
for(j=0;j<2;j++)
printf("%c",b[k++]);
}
k=0;
for(i=0;i<frame;i++)
{
if(i<strlen(usr1))
newusr2[i]=b[k++];
if(i<strlen(usr2))
newusr3[i]=b[k++];
if(i<strlen(usr3))
newusr1[i]=b[k++];
}
newusr1[strlen(usr3)]=NULL;
newusr2[strlen(usr1)]=NULL;
newusr3[strlen(usr2)]=NULL;
printf("\nTransferring Data To Respective Users...\n\n");
printf("user1 gets %s \n",newusr1);
printf("user2 gets %s \n",newusr2);
printf("user3 gets %s \n",newusr3);
return 0;
}