Mar 18, 2010

Monoalphabatic cipher encryption and decryption

//MONOALPHABETIC ENCRYPTION AND DECRYPTION
//copyright jayeshaminnew@gmail.com

//ENCRYPTION OF MONOALPHABAIC CIPHER
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<ctype.h>
void main()
{
FILE *f1,*f2,*f3;
int i,j=0,k,asc[50],l,temp,temp1,flag=0;
char plain[50],cipher[50],key[50],m;
clrscr();
f1=fopen("plain.txt","r");
while((l=getc(f1))!=EOF)
{
plain[j++]=l;
}
plain[j]=NULL;
fclose(f1);
j=0;
f2=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
{
key[j++]=l;
}
key[j]=NULL;
fclose(f2);
cout<<"plain text is\n"<<plain;
cout<<"key is \n"<<key;
//logic
k=0;
for(i=0;i<strlen(plain);i++)
{
asc[k++]=plain[i]-97;
}
k=0;
for(i=0;i<strlen(plain);i++)
{
if(asc[i]>=0 && asc[i]<=25)
cipher[k++]=key[asc[i]];
else
cipher[k++]=plain[asc[i]+97];
}
cout<<"\n"<<"cipher text is\n";
cipher[k]=NULL;
cout<<cipher;

f3=fopen("cipher.txt","w");
for(i=0;i<strlen(cipher);i++)
putc(cipher[i],f2);
getch();
}


//DECRYPTION OF MONOALPHABATIC CIPHER

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<ctype.h>
void main()
{
FILE *f1,*f2,*f3;
int i,j=0,k,asc[50],l,temp,temp1,flag=0;
char plain[50],cipher[50],key[50],m;
clrscr();
f1=fopen("cipher.txt","r");
while((l=getc(f1))!=EOF)
{
cipher[j++]=l;
}
cipher[j]=NULL;
fclose(f1);
j=0;
f2=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
{
key[j++]=l;
}
key[j]=NULL;
fclose(f2);
cout<<"cipher text is\n"<<cipher;
cout<<"\nkey is \n"<<key;
//logic
k=0;
for(i=0;i<strlen(cipher);i++)
{
for(j=0;j<strlen(key);j++)
{
if(cipher[i]==key[j])
break;
}
plain[k++]=j+97;
}
cout<<"\n"<<"plain text is\n";
plain[k]=NULL;
cout<<plain;

f3=fopen("plain.txt","w");
for(i=0;i<strlen(plain);i++)
putc(plain[i],f2);
getch();
}

Caesar cipher encryption and decryption

//CAESAR CIPHER ENCRYPTION
//copyright jayeshaminnew@gmail.com
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
//declaring variables
FILE *f1,*f2;
int i,j=0,k,l,key,temp,temp1,flag=0;
char plain[50],cipher[50],m;
clrscr();
//reading plain text
f1=fopen("plain.txt","r");
while((l=getc(f1))!=EOF)
plain[j++]=l;
plain[j]=NULL;
fclose(f1);
printf("your plain text is:%s\n",plain);
scanf("%d",&key,printf("enter key:"));
for(i=0;i<j;i++)
{
temp=plain[i];
temp1=temp;
temp1+=key;
if(temp1>122 && islower(temp))
temp1-=26;
else if(temp1>90 && isupper(temp))
temp1-=26;
else if(!((temp>=97 && temp<=122) || (temp>=65 && temp<=90)))
{
flag=1;
}
if(flag==0)
cipher[i]=temp1;
else
cipher[i]=temp;
flag=0;
}
cipher[i]=NULL;
//writing cipher text into file
f2=fopen("cipher.txt","w");
for(i=0;i<strlen(cipher);i++)
putc(cipher[i],f2);
fclose(f2);
printf("your cipher text is:%s",cipher);
getch();
}

//CAESAR CIPHER DECRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
void main()
{
FILE *f1,*f2;
int i,j=0,k,l,key,temp,temp1,flag=0;
char plain[50],cipher[50],meaning;
clrscr();
f1=fopen("cipher.txt","r");
while((l=getc(f1))!=EOF)
cipher[j++]=l;
cipher[j]=NULL;
fclose(f1);
j=0;
for(i=0;i<26;i++)
{
for(j=0;j<strlen(cipher);j++)
{
temp=cipher[j];
temp1=temp;
temp1-=i;
if(temp1<97 && islower(temp))
temp1+=26;
else if(temp1<65 && isupper(temp))
temp1+=26;
else if(!((temp>=97 && temp<=122) || (temp>=65 && temp<=90)))
{
flag=1;
}
else
{}
if(flag==0)
plain[j]=temp1;
else
plain[j]=temp;
flag=0;
}
plain[j]=NULL;
printf("Message:%s",plain);
printf("\nis it meaning ful msg?? Y/N?\n");
scanf("%s",&meaning);
if(meaning=='y' || meaning=='Y')
{
printf("your plain text is: %s",plain);
getch();
exit(0);
}
}
getch();
}

Polyalphabatic cipher encryption and decryption

//POLYALPHABATIC CIPHER ENCRYPTION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int start=98,i,temp=97,j,k,m,n,l;
char plain[100],key[100],cipher[100],arr[27][27],ch1,ch2;
FILE *f1,*f2;
clrscr();
//code for matrix generation
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
{
if(temp>122 )
temp=32;
arr[i][j]=temp++;
if(temp==33)
temp=97;
}
temp=start++;
}
//printing matrix
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
printf("%c",arr[i][j]);
printf("\n");
}
i=0;
f1=fopen("plain.txt","r");
while((l=getc(f1))!=EOF)
plain[i++]=l;
plain[i]=NULL;
fclose(f1);
i=0;
f1=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
key[i++]=l;
key[i]=NULL;
fclose(f1);
temp=0;
printf("\nplain text: %s\n",plain);
for(i=strlen(key);i<strlen(plain);i++)
{
if(temp>strlen(key))
temp=0;
key[i]=key[temp++];
}
key[i]=NULL;
printf("key:        %s\n",key);
for(i=0;i<strlen(plain);i++)
{
if(plain[i]!=32)
m=plain[i]-97;
else
m=26;
n=key[i]-97;
cipher[i]=arr[m][n];
}
cipher[i]=NULL;
f1=fopen("cipher.txt","w");
for(i=0;i<strlen(cipher);i++)
putc(cipher[i],f1);
fclose(f1);
printf("cipher text:%s",cipher);
getch();


}

//POLYALPHABATIC CIPHER DECRYPTION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int start=98,i,temp=97,j,k,m,n,l;
char plain[100],key[100],cipher[100],arr[27][27],ch1,ch2;
FILE *f1,*f2;
clrscr();
//code for matrix generation
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
{
if(temp>122 )
temp=32;
arr[i][j]=temp++;
if(temp==33)
temp=97;
}
temp=start++;
}
//printing matrix
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
printf("%c",arr[i][j]);
printf("\n");
}
i=0;
f1=fopen("cipher.txt","r");
while((l=getc(f1))!=EOF)
cipher[i++]=l;
cipher[i]=NULL;
fclose(f1);
i=0;
f1=fopen("key.txt","r");
while((l=getc(f1))!=EOF)
key[i++]=l;
key[i]=NULL;
fclose(f1);
temp=0;
printf("\ncipher text:%s\n",cipher);
for(i=strlen(key);i<strlen(cipher);i++)
{
if(temp>strlen(key))
temp=0;
key[i]=key[temp++];
}
key[i]=NULL;
k=0;
printf("key:        %s\n",key);
for(i=0;i<strlen(cipher);i++)
{
for(j=0;j<27;j++)
{
if(cipher[k]==arr[key[i]-97][j])
break;
}
plain[k++]=arr[0][j];
}
plain[k]=NULL;
f1=fopen("plain.txt","w");
for(i=0;i<strlen(plain);i++)
putc(plain[i],f1);
fclose(f1);
printf("plain text: %s\n",plain);
getch();
}

Bresenham's line drawing algorithm

//This is Bresenham's line drawing algorithm developed in C language
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void jayubre(int,int,int,int);
void main(void)
{
int gdriver = DETECT, gmode,x1,y1,x2,y2;
initgraph(&gdriver, &gmode, "..\\bgi");
printf("Enter starting points(x1,y1):\n"),scanf("%d%d",&x1,&y1);
printf("Enter ending points(x2,y2):\n"),scanf("%d%d",&x2,&y2);
jayubre(x1,y1,x2,y2);
getch();
}
void jayubre(int x1,int y1,int x2,int y2)
{
int p0,dy,dx,sx=1,sy=1,xmax, ymax,x,y;
float m;
putpixel(x1,y1,8);
putpixel(x2,y2,8);
if(x1>x2)
sx=-1;
if(y1>y2)
sy=-1;
dx=abs(x2-x1);
dy=abs(y2-y1);
m=dy/dx;
if(m<1)
{
p0=2*dy-dx;
while(x1!=x2)
{
if(p0<0)
{
p0=p0+2*dy;
x1=x1+sx;
y1=y1;
putpixel(x1,y1,4);
}
else if(p0>=0)
{
p0=p0+2*dy-2*dx;
x1=x1+sx;
y1=y1+sy;
putpixel(x1,y1,4);

}
}
}
else
{
p0=2*dx-dy;
while(y1!=y2)
{
if(p0<0)
{
p0=p0+2*dx;
y1=y1+sy;
x1=x1;
putpixel(x1,y1,4);
}
else if(p0>=0)
{
p0=p0+2*dx-2*dy;
y1=y1+sy;
x1=x1+sx;
putpixel(x1,y1,4);
}
}
}
}

Midpoint circle algorithm

// This is Midpoint circle algorithm developed in C language

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void jayucircle(int xc,int yc,int r);
void jayuprint(int x,int y,int xc,int yc);
void main()
{
   int xc,yc,p,r,x,y,gdriver = DETECT, gmode, errorcode;
   initgraph(&gdriver, &gmode, "..\\bgi");
   printf("enter value of (x,y,r)"),scanf("%d%d%d",&xc,&yc,&r);
   jayucircle(xc,yc,r);
   getch();
}
void jayucircle(int xc,int yc,int r)
{
int x=0,y=r,p=1-r;
jayuprint(0,r,xc,yc);
while(x<=y)
{
if(p<0)
{
x++;
p+=(2*x)+1;
jayuprint(x,y,xc,yc);
}
else
{
x++;
y--;
p+=(2*x)+1-(2*y);
jayuprint(x,y,xc,yc);
}
}
}
void jayuprint(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc-y,7);
putpixel(yc+y,xc+x,7);
putpixel(yc-y,xc+x,7);
putpixel(yc+y,xc-x,7);
putpixel(yc-y,xc-x,7);
}

2D Modeling: 2D Rotation

// This is 2D Rotation program using C language

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float ans[3][10];
void multi(float tmat[3][3],float coormat[3][10],int size);
void main()
{
float coormat[3][10],x[10],y[10],temp;
int gd=DETECT,gm,no,sx,sy,sum=0,i,j,k,theta;
float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}};
clrscr();
initgraph(&gd,&gm,"..\\bgi");
printf("enter theta:"),scanf("%d",&theta);
temp=(float)theta*3.14/180;
tmat[0][0]=cos(temp);
tmat[1][1]=cos(temp);
tmat[0][1]=-1*sin(temp);
tmat[1][0]=-1*sin(temp);
printf("Translation matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4.1f\t",tmat[i][j]);
printf("\n");
}
printf("enter no. of vertices:\n"),scanf("%d",&no);
for(i=0;i<no;i++)
printf("enter (x%d,y%d):\n",i+1,i+1),scanf("%f%f",&x[i],&y[i]);
for(i=0;i<no;i++)
coormat[0][i]=x[i];
for(i=0;i<no;i++)
coormat[1][i]=y[i];
for(i=0;i<no;i++)
coormat[2][i]=1;
printf("co ordinate matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2f\t",coormat[i][j]);
printf("\n");
}
//printing starting line
for(j=0;j<no-1;j++)
line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]);
line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]);
multi(tmat,coormat,no);
printf("answer matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2f\t",ans[i][j]);
printf("\n");
}

for(j=0;j<no-1;j++)
line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]);
line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]);

getch();
}
void multi(float tmat[3][3],float coormat[3][10],int no)
{
int i,j,k,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum+=tmat[i][k]*coormat[k][j];
}
ans[i][j]=sum;
}
}
}

2D Modeling: 2D scaling

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float ans[3][10];
void multi(float tmat[3][3],float coormat[3][10],int size);
void main()
{
float coormat[3][10],x[10],y[10];
int gd=DETECT,gm,no,sx,sy,sum=0,i,j,k;
float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}};
clrscr();
initgraph(&gd,&gm,"..\\bgi");
printf("enter (sx,sy):"),scanf("%d%d",&sx,&sy);
tmat[0][0]=sx;
tmat[1][1]=sy;
printf("Translation matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4.1f\t",tmat[i][j]);
printf("\n");
}
printf("enter no. of vertices:\n"),scanf("%d",&no);
for(i=0;i<no;i++)
printf("enter (x%d,y%d):\n",i+1,i+1),scanf("%f%f",&x[i],&y[i]);
for(i=0;i<no;i++)
coormat[0][i]=x[i];
for(i=0;i<no;i++)
coormat[1][i]=y[i];
for(i=0;i<no;i++)
coormat[2][i]=1;
printf("co ordinate matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2f\t",coormat[i][j]);
printf("\n");
}
//printing starting line
for(j=0;j<no-1;j++)
line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]);
line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]);
multi(tmat,coormat,no);
printf("answer matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2f\t",ans[i][j]);
printf("\n");
}

for(j=0;j<no-1;j++)
line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]);
line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]);

getch();
}
void multi(float tmat[3][3],float coormat[3][10],int no)
{
int i,j,k,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum+=tmat[i][k]*coormat[k][j];
}
ans[i][j]=sum;
}
}
}

2D Modeling: 2D transformation

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float ans[3][10];
void multi(float tmat[3][3],float coormat[3][10],int size);
void main()
{
float coormat[3][10],x[10],y[10];
int gd=DETECT,gm,no,tx,ty,sum=0,i,j,k;
float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}};
clrscr();
initgraph(&gd,&gm,"..\\bgi");
printf("enter (tx,ty):"),scanf("%d%d",&tx,&ty);
tmat[0][2]=tx;
tmat[1][2]=ty;
printf("Translation matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4.1f\t",tmat[i][j]);
printf("\n");
}
printf("enter no. of vertices:\n"),scanf("%d",&no);
for(i=0;i<no;i++)
printf("enter (x%d,y%d):\n",i+1,i+1),scanf("%f%f",&x[i],&y[i]);
for(i=0;i<no;i++)
coormat[0][i]=x[i];
for(i=0;i<no;i++)
coormat[1][i]=y[i];
for(i=0;i<no;i++)
coormat[2][i]=1;
printf("co ordinate matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2f\t",coormat[i][j]);
printf("\n");
}
//printing starting line
for(j=0;j<no-1;j++)
line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]);
line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]);
multi(tmat,coormat,no);
printf("answer matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2f\t",ans[i][j]);
printf("\n");
}

for(j=0;j<no-1;j++)
line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]);
line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]);

getch();
}
void multi(float tmat[3][3],float coormat[3][10],int no)
{
int i,j,k,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum+=tmat[i][k]*coormat[k][j];
}
ans[i][j]=sum;
}
}
}

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;
}