私は matlab プログラミングが初めてで、RC4 コードを使用しようとしています [ソース: http://www.cryptosmith.com/archives/621 ]。キーで暗号化されたメッセージを取得しようとしていますが、キー ストリームを取得した後、データの xor を実行中にスタックします。私の結果はずっと0を示しているので、おそらくいくつかの場所で間違いを犯しています。私が問題を抱えているメイク関数コードは次のとおりです。
function w = rc4make(n,k)
% rc4make - makes a vector of "n" RC4 outputs of key "k"
sc = rc4key(k);
l = [];
j0 = 0;
i0 = 0;
for s0 = 1:n
[r, i0, j0, sc]=rc4out(i0, j0, sc);
l =[l r];
L1 = logical(n);
disp(L1);
L2 = logical(l);% converting into logical array
disp(L2);
w = xor(L1,L2);
end
function sc=rc4key(key)
% rc4key - return key schedule array for key k
% SEEMS BROKEN - bytes 2-9 are swapped with other key schedule bytes
% At best, not compatible with 'real' RC4. At worst, also more vulnerable
% set up the array
le = length(key);
sc = 0:255;
j0 = 0;
% scramble the key schedule
for i0 = 0:255
k0 = floor(key( floor(mod(i0,le))+1 ));%floor rounds the number into round figure
j0 = floor(mod( j0 + k0 + sc(i0+1), 256));
tm = sc(i0+1);
sc(i0+1) = sc(j0+1);
sc(j0+1) = tm;
end
function [r, i0, j0, sc]=rc4out(i0, j0, sc)
% next byte of rc4 output
% inputs: i0, j0 = indices; sc = key schedule
% outputs: r=random byte; i0, j0 = indices; sc = key schedule
%for q=0:strlen(data)
i0 = mod( (i0+1), 256);
j0 = mod( j0 + sc(i0+1), 256);
tmp = sc(j0+1);
sc(j0+1) = sc(i0+1);
sc(i0+1) = tmp;
r = mod(sc(i0+1) + sc(j0+1), 256);%(S[i]+S[j]) %256
私が呼び出している関数は次のとおりです。rc4make(12,'hi')
ここで、12 はプレーンテキストで、hi はキーです。私のコードの問題を理解するように私を導き、暗号文を適切に取得することを提案してください。