Playfair Cipher

2
13218

Playfair cipher is invented by Charles Wheatstone in 1854, but named after his friend Baron Playfair.The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher.In monoalphabatic cipher,not even large number of keys provide security.To improve security one way is to encrypt multiple letters.

Its Matlab Implementation:

[code lang=”matlab”]
function playfair
clc
clear all
close all
disp(‘Play fair cipher’)
disp(‘KEY WORD for playfair cipher : ‘)
key=’KEYWORD’
key_len=length(key);

matrix=zeros(5,5);

num=[];
for j=1:26
alphabets(j)=char(64+j)
end
for q=1:key_len
num=double(key(q))
for h=1:26
ch=double(alphabets(h))
if (num==ch)
alphabets(h)=0
end
end
end
q=1;
for m=1:26
ff=double(alphabets(m))
if((ff~=0)&&(ff~=73)&&(ff~=74))
new_alpha(q)=alphabets(m)
q=q+1;
end
end
len=length(new_alpha)
f=25
words_comb=zeros(1,f)

for m=1:f
if(key_len>=m)
words_comb(m)=key(m)
else(m>key_len)
for h=1:len
words_comb(h+key_len)=new_alpha(h)
end
end
end
for i=1:f-1
ff=char(words_comb(i))
mat_words(i)=ff
end
len_mat=length(mat_words)
h=1

for (i=1:5)
for j=1:5
try
matrix(i,j)=mat_words(h)

h=h+1;
catch(h<25)
break
end
end
end
for i=1:5
for j=1:5
b=char(matrix(i,j))
words(i,j)={b}
end
end
words(5,5)={‘I/J’}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sentence=’WHYDONTYOU’
s_len=length(sentence)
for i=1:s_len/2
pair{i}=sentence(i*2-1:i*2);
end
pair
for i=1:s_len/2
pair1=sentence(i*2-1);
pair2=sentence(i*2);
pair_all1{i}=pair1;
pair_all2{i}=pair2;
end
pair_all1
pair_all2
le=length(pair_all1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for k=1:le
y=pair_all1{k};
z=pair_all2{k};
disp(‘…………………………….count…………………………….’)
for i=1:5
for j=1:5
b=words{i,j};
if(b==y)
mj(k)=i
aq(k)=j
end
if(b==z)
ma(k)=i
kh(k)=j
end
end
end
end

for k=1:le
if (mj(k)<ma(k)) p(k)=mj(k); q(k)=ma(k); elseif (mj(k)==ma(k)) p(k)=mj q(k)=mj(k) e=mod(k+1,5); r(k)=aq(e) s(k)=kh(e) else (mj(k)>ma(k))
p(k)=ma(k);
q(k)=mj(k);
end
if (aq(k)<kh(k)) r(k)=aq(k); s(k)=kh(k); elseif (aq(k)==kh(k)) o=mod(k+1,5); r(k)=aq(k) s(k)=aq(k) p(k)=mj(o) q(k)=ma(o) else (aq(k)>kh(k))
r(k)=kh(k);
s(k)=aq(k);
end
p1{k}=words{p(k),r(k)};
p2{k}=words{q(k),s(k)};

end
p1
p2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
playfair={p1{1},p2{1},p1{2},p2{2},p1{3},p2{3},p1{4},p2{4},p1{5},p2{5}}
[/code]

LEAVE A REPLY

Please enter your comment!
Please enter your name here