RC4 matlab code conversion not working

三世轮回 提交于 2019-12-31 05:25:24

问题


I have been trying to implement RC4 in Matlab but due to unknown reason the following code is not giving the right output. Please give me suggestion on improving this Matlab code:

function ef = rc4full(pf,ki)
s = rc4key(ki);
disp(s);
s = uint8(s);
j0 = 0;
i0 = 0;
r = prga(s, pf);
disp(r);
v = uint8(pf);
C = bitxor(v,r);
disp(C);
data_show =dec2hex(C);
ef = data_show;

function sc=rc4key(key)
for i0 = 0:255
    sc(i0+1) = i0+1;
end
j0 = 0;
for i0 = 0:255
    j0 = mod(j0 + sc(i0+1) + key(mod(i0, length(key)) + 1), 256);
    tmp = sc(i0+1);
    sc(i0+1) = sc(j0+1);
    sc(j0+1) = tmp;
end

function r = prga(sc, data)
i0=0; j0=0; x=[]; t=[];
for x=0:length(data)-1
    i0 = mod( (i0+1), 256);
    j0 = mod( j0 + sc(i0+1), 256);
    tmp = sc(i0+1);
    sc(i0+1) = sc(j0+1);
    sc(j0+1) = tmp;
    r(x+1) = sc(mod( sc(i0+1) + sc(j0+1), 256)+1);
end

what I am expecting: Suppose I tried 'Hello' as data and 'Hi' as key. then in online it shows this: c9 9f 8d 97 91 but I am getting: af fe 50 1e ff.

来源:https://stackoverflow.com/questions/20777855/rc4-matlab-code-conversion-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!