-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayfair.c
159 lines (148 loc) · 3.22 KB
/
Playfair.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
--Playfair encryption method--
developed by neel patel
*/
#include<string.h>
#include<stdlib.h>
#define no '@'
char mat[5][5]; //matrix used in encryption
int conv(char *s); //process string s
int checkmat(char); //find character in matrix
char inc(char c); //increment character c
int getrow(char c);
int getcol(char c);
void fillmat(char *k); //fill the matrix
char * playfair(char *st,int n);//apply encryption on string st
void main(){
int i,j,n;
char key[27],*text,*ct;
printf("enter keyword :- ");
scanf("%s",key);
fillmat(key);
printf("\nmatrix :- \n");
for(i=0;i<5;i++){//print matrix
for(j=0;j<5;j++)
printf("%c ",mat[i][j]);
printf("\n");
}
printf("\nenter length of text..\n");
scanf("%d",&n);
text=(char *)calloc(n+1,sizeof(char));
printf("\nenter text..\n");
scanf("%s",text);
text[n]=NULL;
ct=playfair(text,n);
printf("cypher text :- %s",ct);
}
int conv(char *s){//convert string to lower case & replace j with i, return length of string
int i;
for(i=0;s[i]!=NULL;i++){
if(s[i]=='j')
s[i]='i';
s[i]=tolower(s[i]);
}
return i;
}
void fillmat(char *s){//fill the matrix with keyword then rest of space will be filled by remaining characters
int i,j,k;
char r='a';
conv(s);
{//fill the matrix with NULL
for(i=0;i<5;i++)
for(j=0;j<5;j++)
mat[i][j]=no;
}
{//enter key in matrix
for(i=0,k=0;i<5&&s[k]!=NULL;i++)
for(j=0;j<5&&s[k]!=NULL;j++){
for(;s[k]!=NULL&&checkmat(s[k]);k++);
if(s[k]!=NULL)
mat[i][j]=s[k];
}
}
{//fill rest of matrix
r='a';
for(i=0;i<5&&r<='z';i++)
for(j=0;j<5&&r<='z';j++){
if(mat[i][j]==no){
for(;checkmat(r);r=inc(r));
mat[i][j]=r;
}
}
}
}
int checkmat(char c){//return index if c exist in matrix otherwise return 0
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(mat[i][j]==c)
return i*5+j+1;
return 0;
}
char inc(char c){//increment c, if new value of c is j then again increment c, return null if c is not alpha
c++;
if(c=='j')
c++;
if(c>='a'&&c<='z')
return c;
return NULL;
}
char * playfair(char *s,int n){//apply encryption on string s using matrix mat, return cypher text
int i,c,r,c0,c1,f,j;
char *st;
st=(char *)calloc(n*2,sizeof(char));
conv(s);
for(i=0,j=0;s[j]!=NULL&&j<n;i+=2){
if(s[j+1]==NULL||s[j]==s[j+1]){//if two letters are same or single element then add 'x'.
c0=s[j];
c1='x';
j++;
}
else{
c0=s[j];
c1=s[j+1];
j+=2;
}
if(getrow(c0)==getrow(c1)){//both elements in same row
c=getcol(c0);
r=getrow(c0);
st[i]=mat[r][(c+1)%5];
c=getcol(c1);
r=getrow(c1);
st[i+1]=mat[r][(c+1)%5];
}
else if(getcol(c0)==getcol(c1)){//both elements in same column
c=getcol(c0);
r=getrow(c0);
st[i]=mat[(r+1)%5][c];
c=getcol(c1);
r=getrow(c1);
st[i+1]=mat[(r+1)%5][c];
}
else{//both elements in different column & row
r=getrow(c0);
c=getcol(c1);
st[i]=mat[r][c];
r=getrow(c1);
c=getcol(c0);
st[i+1]=mat[r][c];
}
}
return st;
}
int getrow(char c){//return row of c in mat
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(c==mat[i][j])
return i;
return -1;
}
int getcol(char c){//return column of c in mat
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(c==mat[i][j])
return j;
return -1;
}