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

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.
this is the full code and the error.. can you help me fix the error? thank you.
Not enough input arguments.
Error in DES (line 46) if numel(input64) == 64 && isempty(setdiff(unique(input64),[0,1]))
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.
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?

Sign in to comment.

 Accepted Answer

The error you posted:
Error in DES (line 46)
if numel(input64) == 64 && isempty(setdiff(unique(input64),[0,1]))
means that you are not passing the ‘input64’ argument to the ‘DES’ function. This may mean that you are not passing any other arguments to it either.
You need to create a script with the arguments defined in your code before you call ‘DES’. You cannot call it correctly by clicking on the green triangle in the Editor.
Try something like this in your script file that calls the ‘DES’ function:
input64 = ; % Provide Appropriate Value
mode = ; % Provide Appropriate Value
key = ; % Provide Appropriate Value
ReturnedOutput = DES(input64,mode,key);

More Answers (1)

Image Analyst
Image Analyst on 29 Apr 2017
The answer to fixing virtually all coding problems can be found here
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

(1,3,nargin), i have seen the examples on another discussion that tell to input these 3 values. it works using matlab2012, but also has error from another line of the code. and when i am trying to use matlab2015 it does not work.
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!