0

我是 matlab 编程新手并尝试使用 RC4 代码 [来源:http ://www.cryptosmith.com/archives/621 ]。我正在尝试使用密钥加密消息,但是在获得密钥流之后,我在对数据进行异或时卡住了。我可能在几个地方都犯了错误,因为我的结果始终显示为 0。我遇到问题的make函数代码是:

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 是密钥。您能否指导我理解我的代码问题,并建议正确获取密文。

4

0 回答 0