Hill-playfair - WordPress.com

advertisement
Playfair Cipher
The Algorithm
The 'key' for a playfair cipher is generally a word, for the sake of example we will choose
'monarchy'. This is then used to generate a 'key square', e.g.
m
c
e
l
u
o
h
f
p
v
n
y
g
q
w
a
b
i
s
x
r
d
k
t
z
Any sequence of 25 letters can be used as a key, so long as all letters are in it and there are no
repeats. Note that there is no 'j', it is combined with 'i'. We now apply the encryption rules to
encrypt the plaintext.
1. Remove any punctuation or characters that are not present in the key square (this may
mean spelling out numbers, punctuation etc.).
2. Identify any double letters in the plaintext and replace the second occurence with an 'x'
e.g. 'hammer' -> 'hamxer'.
3. If the plaintext has an odd number of characters, append an 'x' to the end to make it even.
4. Break the plaintext into pairs of letters, e.g. 'hamxer' -> 'ha mx er'
5. The algorithm now works on each of the letter pairs.
6. Locate the letters in the key square, (the examples given are using the key square above)
a. If the letters are in different rows and columns, replace the pair with the letters on
the same row respectively but at the other pair of corners of the rectangle defined
by the original pair. The order is important – the first encrypted letter of the pair is
the one that lies on the same row as the first plaintext letter. 'ha' -> 'bo', 'es' -> 'il'
b. If the letters appear on the same row of the table, replace them with the letters to
their immediate right respectively (wrapping around to the left side of the row if a
letter in the original pair was on the right side of the row). 'ma' -> 'or', 'lp' -> 'pq'
c. If the letters appear on the same column of the table, replace them with the letters
immediately below respectively (wrapping around to the top side of the column if
a letter in the original pair was on the bottom side of the column). 'rk' -> 'dt', 'pv' > 'vo'
Clarification with pictures - Assume one wants to encrypt the digraph OR. There are three
general cases: [1]
1.
2.
3.
4.
5.
m
*
*
l
*
*
*
*
*
*
*
*
*
*
*
a
*
*
s
*
*
*
*
*
*
Hence, al -> ms
6. *
7. *
8. *
9. *
10.
*
h
*
*
*
*
y
*
*
*
*
b
*
*
*
*
d
*
*
* *
Hence, hb -> yd
11.
12.
13.
14.
15.
*
*
*
*
*
*
*
*
*
*
n
y
*
q
w
*
*
*
*
*
*
*
*
*
*
Hence, nq -> yw
An example encryption, "we are discovered, save yourself" using the key square shown at the
beginning of this section:
plaintext: wearediscoveredsaveyourselfx
ciphertext: ugrmkcsxhmufmkbtoxgcmvatluiv
//Aim:To implement Playfair cipher in C.
//Program:
#include<stdio.h>
int check(char table[5][5],char k)
{
int i,j;
for(i=0;i<5;++i)
for(j=0;j<5;++j)
{
if(table[i][j]==k)
return 0;
}
return 1;
}
void main()
{
int i,j,key_len;
char table[5][5];
for(i=0;i<5;++i)
for(j=0;j<5;++j)
table[i][j]='0';
printf("**********Playfair Cipher************\n\n");
printf("Enter the length of the Key. ");
scanf("%d",&key_len);
char key[key_len];
printf("Enter the Key. ");
for(i=-1;i<key_len;++i)
{
scanf("%c",&key[i]);
if(key[i]=='j')
key[i]='i';
}
int flag;
int count=0;
// inserting the key into the table
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
flag=0;
while(flag!=1)
{
if(count>key_len)
goto l1;
flag=check(table,key[count]);
++count;
}// end of while
table[i][j]=key[(count-1)];
}// end of inner for
}// end of outer for
l1:printf("\n");
int val=97;
//inserting other alphabets
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]>=97 && table[i][j]<=123)
{}
else
{
flag=0;
while(flag!=1)
{
if('j'==(char)val)
++val;
flag=check(table,(char)val);
++val;
}// end of while
table[i][j]=(char)(val-1);
}//end of else
}// end of inner for
}// end of outer for
printf("The table is as follows:\n");
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
printf("%c ",table[i][j]);
}
printf("\n");
}
int l=0;
printf("\nEnter the length length of plain text.(without spaces) ");
scanf("%d",&l);
printf("\nEnter the Plain text. ");
char p[l];
for(i=-1;i<l;++i)
{
scanf("%c",&p[i]);
}
for(i=-1;i<l;++i)
{
if(p[i]=='j')
p[i]='i';
}
printf("\nThe replaced text(j with i)");
for(i=-1;i<l;++i)
printf("%c ",p[i]);
count=0;
for(i=-1;i<l;++i)
{
if(p[i]==p[i+1])
count=count+1;
}
printf("\nThe cipher has to enter %d bogus char.It is either 'x' or 'z'\n",count);
int length=0;
if((l+count)%2!=0)
length=(l+count+1);
else
length=(l+count);
printf("\nValue of length is %d.\n",length);
char p1[length];
//inserting bogus characters.
char temp1;
int count1=0;
for(i=-1;i<l;++i)
{
p1[count1]=p[i];
if(p[i]==p[i+1])
{
count1=count1+1;
if(p[i]=='x')
p1[count1]='z';
else
p1[count1]='x';
}
count1=count1+1;
}
//checking for length
char bogus;
if((l+count)%2!=0)
{
if(p1[length-1]=='x')
p1[length]='z';
else
p1[length]='x';
}
printf("The final text is:");
for(i=0;i<=length;++i)
printf("%c ",p1[i]);
char cipher_text[length];
int r1,r2,c1,c2;
int k1;
for(k1=1;k1<=length;++k1)
{
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]==p1[k1])
{
r1=i;
c1=j;
}
else
if(table[i][j]==p1[k1+1])
{
r2=i;
c2=j;
}
}//end of for with j
}//end of for with i
if(r1==r2)
{
cipher_text[k1]=table[r1][(c1+1)%5];
cipher_text[k1+1]=table[r1][(c2+1)%5];
}
else
if(c1==c2)
{
cipher_text[k1]=table[(r1+1)%5][c1];
cipher_text[k1+1]=table[(r2+1)%5][c1];
}
else
{
cipher_text[k1]=table[r1][c2];
cipher_text[k1+1]=table[r2][c1];
}
k1=k1+1;
}//end of for with k1
printf("\n\nThe Cipher text is:\n ");
for(i=1;i<=length;++i)
printf("%c ",cipher_text[i]);
}
/*OUTPUT
[aditya@localhost Desktop]$ gcc playfair.c
[aditya@localhost Desktop]$ ./a.out
**********Playfair Cipher************
Enter the length of the Key. 15
Enter the Key. playfairexample
The table is as follows:
playf
irexm
bcdgh
knoqs
tuvwz
Enter the length length of plain text.(without spaces) 25
Enter the Plain text. hidethegoldinthetreestump
The replaced text(j with i)
hidethegoldinthetreestump
The cipher has to enter 1 bogus char.It is either 'x' or 'z'
Value of length is 26.
The final text is:
hidethegoldinthetrexestump
The Cipher text is:
bmodzbxdnabekudmuixmmouvif
*/
Hill Cipher Algorithm Program in C
/*Hill Cipher Algorithm Program in C */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
clrscr();
printf("\nEnter the string : ");
scanf("%s",txt);
//clrscr();
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
//printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
//printf("final : %d ",txt[i]);
//printf("\n\n");
}
clrscr();
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
for(i=0;i<end;i++)
{
sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
}
ans[i][0]=sum;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
getch();
}
Download