not enough input arguments
Show older comments
how to fix the problem?
function [varargout] = DES(input64,mode,key)
error(nargchk(1,3,nargin));
switch nargin
case 1
mode = 'ENC';
K = round(rand(8,7));
K(:,8) = mod(sum(K,2),2); % note these eight bits of key are never used in encryption
K = reshape(K',1,64);
varargout{2} = K;
case 2
switch mode
case 'ENC'
K = round(rand(8,7));
K(:,8) = mod(sum(K,2),2); % note these eight bits of key are never used in encryption
K = reshape(K',1,64);
varargout{2} = K;
case 'DEC'
error('Key has to be provided in decryption mode (DEC)')
otherwise
error('WRONG working mode!!! Select either encrtyption mode: ENC or decryption mode: DEC !!!')
end
case 3
if isempty(setdiff(unique(key),[0,1])) % check provided key type
if numel(key) == 64 % check provided key parity
keyParityCheck = @(k) (sum(mod(sum(reshape(k,8,8)),2))==0);
if keyParityCheck(key) == 1
K = key(:)';
else
error('Key parity check FAILED!!!')
end
elseif numel(key) == 56 % add parity bits
K = reshape(key,7,8)';
K(:,8) = mod(sum(K,2),2); % note these eight bits of key are never used in encryption
K = reshape(K',1,64);
varargout{2} = K;
display('Key parity bits added')
else
error('Key has to be either 56 or 64-bit long!!!')
end
else
error('Key has to be binary!!!')
end
4 Comments
Star Strider
on 29 Apr 2017
What is the exact error?
How are you calling your function? Are you calling it from a script, from the Command Window, or are your using the green ‘Run’ arrow in the MATLAB Editor?
Copy all the red error output from your Command Window and paste it in a comment here. We cannot run your code, particularly because we do not know what the inputs should be to your function, so we cannot reproduce the error ourselves.
nurul esniah kamaruddin
on 29 Apr 2017
Edited: nurul esniah kamaruddin
on 29 Apr 2017
Star Strider
on 29 Apr 2017
Please see my previous Comment.
I can help you fix the error if I am certain what it is and where it occurs in your code. I need the complete red error message from your Command Window first.
Walter Roberson
on 29 Apr 2017
You have posted code for a function, but you have not told us how you are invoking the function. How are you telling MATLAB that you want to execute the function?
Accepted Answer
More Answers (1)
Image Analyst
on 29 Apr 2017
0 votes
What are you passing in for input64,mode,key? You're not just passing in nothing and simply hitting the green run triangle (or hitting F5) are you? You need to pass in exactly 3 arguments because that's what the function was written to require. What are your 3 values?
2 Comments
nurul esniah kamaruddin
on 29 Apr 2017
Image Analyst
on 29 Apr 2017
Your comments above to Star do not help. What he and I both asked in in my question. How did you run it, and did you pass anything in for the arguments?
Saying " it works using matlab2012, but also has error from another line of the code" is a contradiction. If it works in R2012 there will be no error message.
Categories
Find more on Programming 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!