This is an extension of the previously asked question: link. In a short, I am trying to convert a C program into Matlab and looking for your suggestion to improve the code as th
EDIT:
Try this it do the same think as the c function: The lengh of S
should be >= 256
or you will exceed it as mod()
could return such index. I suggest you to change 256
in the function with the lenght
provided to solve this issue.
Here you don't need the key
variable.
function out = prga(S, len)
i=0; j=0; x=[]; t=[];
for x=0:len-1
i = mod(i+1, 256);
j = mod(j+S(i+1), 256);
t = S(i+1);
S(i+1) = S(j+1);
S(j+1) = t;
out(x+1) = S(mod(S(i+1)+S(j+1), 256)+1);
end
end
Or you can use the key
variable to controle the loop
function out = prga(S, key)
i=0; j=0; x=[]; t=[];
for x=0:length(key)-1
i = mod(i+1, 256);
j = mod(j+S(i+1), 256);
t = S(i+1);
S(i+1) = S(j+1);
S(j+1) = t;
out(x+1) = S(mod(S(i+1)+S(j+1), 256)+1);
end
end