converting the c code to matlab code
Show older comments
can anyone please help me in converting the c code of playfair cipher to matlab. This would be really very helpfull to me.
4//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]='T';
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: p l a y f i r e x m b c d g h k n o q s t u v w z
Enter the length length of plain text.(without spaces) 25
Enter the Plain text. hidethegoldinthetreestump
The replaced text(j with i) h i d e t h e g o l d i n t h e t r e e s t u m p
The cipher has to enter 1 bogus char.It is either 'x' or 'z'
Value of length is 26. The final text is: h i d e t h e g o l d i n t h e t r e x e s t u m p
The Cipher text is: b m o d z b x d n a b e k u d m u i x m m o u v i f
*/
1 Comment
Guillaume
on 16 Mar 2015
I think you're dreaming if you think somebody is going to do this for you.
Why don't you give it a go and come back to ask question when you encounter a non-trivial problem.
Answers (1)
Rameshwar
on 9 Jan 2025
0 votes
% Function to check if character exists in the Playfair table
function result = check(table, k)
result = 1; % Assume not found
[rows, cols] = size(table);
for i = 1:rows
for j = 1:cols
if table(i, j) == k
result = 0; % Found
return;
end
end
end
end
% Main function
function playfairCipher()
% Initialize variables
clc;
clear;
disp('********** Playfair Cipher ************');
% Read key length and key
key_len = input('Enter the length of the Key: ');
key = input('Enter the Key: ', 's');
% Replace 'j' with 'i' in the key
key = strrep(key, 'j', 'i');
% Initialize Playfair table
table = repmat('0', 5, 5);
count = 1; % To track the key characters
% Fill Playfair table with the key characters
for i = 1:5
for j = 1:5
flag = 0;
while flag ~= 1
if count > key_len
break;
end
if check(table, key(count))
table(i, j) = key(count);
flag = 1;
end
count = count + 1;
end
end
end
% Insert remaining alphabet (excluding 'j') into the table
alphabet = 'abcdefghijklmnopqrstuvwxyz';
val = 1; % For alphabet insertion
for i = 1:5
for j = 1:5
if table(i, j) == '0'
while check(table, alphabet(val)) == 1
table(i, j) = alphabet(val);
val = val + 1;
if val > 9
val = val + 1; % Skip 'j'
end
end
end
end
end
% Display the table
disp('The table is as follows:');
disp(table);
% Read plaintext length and the plaintext
l = input('Enter the length of plain text (without spaces): ');
p = input('Enter the Plain text: ', 's');
% Replace 'j' with 'i' in plaintext
p = strrep(p, 'j', 'i');
disp('The replaced text (j with i):');
disp(p);
% Add padding 'x' if needed
count = sum(p(1:end-1) == p(2:end)); % Count identical consecutive characters
disp(['The cipher has to insert ', num2str(count), ' bogus char(s).']);
% Adjust length for even number of characters
length = l + count;
if mod(length, 2) ~= 0
length = length + 1;
end
p1 = p; % Temporary array to hold padded text
count1 = 1;
% Insert 'x' or 'z' for consecutive same characters
for i = 1:l-1
if p(i) == p(i+1)
p1(count1 + 1) = 'x'; % Padding with 'x'
count1 = count1 + 2;
else
p1(count1) = p(i);
count1 = count1 + 1;
end
end
% If the length is odd, add an 'x' at the end
if mod(length, 2) == 0
p1(length) = p1(length - 1);
else
p1(length) = 'x';
end
disp('The final text is:');
disp(p1);
% Prepare to encrypt the text
cipher_text = char(zeros(1, length));
for k = 1:2:length
% Find positions of the two characters in the Playfair table
[r1, c1] = find(table == p1(k));
[r2, c2] = find(table == p1(k+1));
% Apply Playfair cipher rules
if r1 == r2
cipher_text(k) = table(r1, mod(c1, 5) + 1);
cipher_text(k+1) = table(r1, mod(c2, 5) + 1);
elseif c1 == c2
cipher_text(k) = table(mod(r1, 5) + 1, c1);
cipher_text(k+1) = table(mod(r2, 5) + 1, c1);
else
cipher_text(k) = table(r1, c2);
cipher_text(k+1) = table(r2, c1);
end
end
% Display cipher text
disp('The Cipher text is:');
disp(cipher_text);
end
% Run the Playfair Cipher function
playfairCipher();
Categories
Find more on Encryption / Cryptography in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!