Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to implement RC4 encryptionhttp://en.wikipedia.org/wiki/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:

C++
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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900