can someone help me? when I want to insert the plaintext and also the matrix to the hill cipher, syntax out: Error using hillspasi (line 2) Not enough input arguments. is there something wrong in my code?

2 ビュー (過去 30 日間)
Faiz wu
Faiz wu 2020 年 11 月 3 日
回答済み: Shaunak 2025 年 1 月 23 日
function hillspasi(x,k)
[r,c]=size(k);
if r~=c %jika jumlah baris tidak sama dengan jumlah kolom
disp('Kunci harus matrik bujur sangkar')
else
data=upper(x);
if ismember(' ',data)==1 %cek inputan mengandung spasi
spasi=find(data==' ');
km=size(spasi,2);
else
spasi=0;
end;
n=size(data,2);
xx=[];
a=0;
for i=1:n %menghilangkan spasi pada input jika ada
if data(i)~=' '
a=a+1;
xx=[xx data(i)];
end;
end;
s=mod(a,r);
if s~=0
ss=r-s;
sss=[];
for i=1:ss
sss=[sss 'X' ];
end;
xx=[xx sss];
a=a+ss;
else
ss=0;
end;
i=1;
h=[];
while i<=a
m=[];
for j=i:(i-1+r)
m=[m xx(j)];
end;
m=double(m)-65;
c=mod(k*m',26);
c=(c+65)';
h=[h char(c)];
i=i+r;
end;
if spasi~=0
hx=[];j=1;kk=0;
sj=spasi(j);
for i=1:n+ss
if i==sj
hx=[hx ' '];
j=j+1;
if j<=km
sj=spasi(j);
end;
else
kk=kk+1;
if kk<=a
d=h(kk);
hx=[hx d];
end;
end;
end;
else
hx=h;
end;
hh=[];
for i=1:n
hh=[hh hx(i)];
end;
disp('CIPHER : ')
disp('~~~~~~~~')
disp(hx)
if ~isequal(hx,hh)
disp(' ')
disp('CIPHER DENGAN MENGHILANGKAN KARAKTER TAMBAHAN :')
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
disp(hh)
end;
end;

回答 (1 件)

Shaunak
Shaunak 2025 年 1 月 23 日
Hello Faiz,
The error message "Not enough input arguments" typically indicates that the function is being invoked incorrectly, rather than an issue with the function's definition itself.
To resolve this, ensure that the number of arguments you pass when calling the function matches the number of parameters specified in the function's definition.
Below is an example for your reference:
% Example plaintext
plaintext = 'HELLO WORLD';
% Example key matrix (ensure it's a square matrix)
keyMatrix = [3 3; 2 5];
% Call the function with both arguments
hillspasi(plaintext, keyMatrix);
By ensuring that both the plaintext and the key matrix are provided as arguments, you can avoid this common error and ensure the function operates as intended.
Please refer to this documentation for more information on function calling:
https://www.mathworks.com/help/matlab/learn_matlab/calling-functions.html

カテゴリ

Help Center および File ExchangeEncryption / Cryptography についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by