MATLAB Answers

This the main file for hill cipher

1 view (last 30 days)
nishant tomar
nishant tomar on 19 Jul 2021
Commented: nishant tomar on 19 Jul 2021
function [destr] = Hill_Cipher()
message = input('Input Message without spaces: ', 's');
len = length(message);
while f==1
disp('Enter The Key');
for i=1:3
for j=1:3
key(i,j)=input('element-');
end
end
detkey = det(key);
if mod(detkey,2)==0
disp('enter key with odd determinant');
f=1;
else
f=0;
end
end
amessage = double(message);
modlen = mod(len,3);
if mod(len,3) ~= 0
for i=1:3 - mod(len,3)
amessage(1,len+i) = 32;
end
end
bmessage = reshape(amessage, 3, ceil(len/3));
for i=1:ceil((len/3))
for j=1:3
bmessage(j,i)= bmessage(j,i)-33;
end
end
en_message = Encrypt(key,bmessage);
twoen_message = en_message;
for i=1:ceil(len/3)
for j=1:3
en_message(j,i)= en_message(j,i)+33;
end
end
enstr = reshape(char(en_message),1,j*i);
disp(enstr);
de_message = Decrypt(key,twoen_message);
for i=1:ceil(len/3)
for j=1:3
de_message(j,i)= de_message(j,i)+33;
end
end
destr = reshape(char(de_message),1,j*i);
if modlen ~= 0
destr = destr(1:len);
end
end
  1 Comment
KSSV
KSSV on 19 Jul 2021
Few variables and functions are not defined inside the code. You may learn about it referring this: https://www.geeksforgeeks.org/hill-cipher/

Sign in to comment.

Answers (1)

DGM
DGM on 19 Jul 2021
uhh okay?
f = 1;
But if you really want my advice, asking the user to input a bunch of things one element at a time using input() is nothing but incredibly tedious and confusing. It's an unnecessary invitation for frustration and error. Functions take arguments. Your function needs two arguments - a char vector and a 3x3 matrix. Take then from the function call instead of pestering the user to retype everything repeatedly.
... and if you don't want spaces in the message, just strip them instead of relying on the input to be perfect.
message = message(message~=' ');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!