在matlab中将double和integer值转换为逻辑数组

时间:2013-12-09 04:17:44

标签: matlab xor rc4-cipher

我是matlab编程的新手,并尝试使用RC4代码[来源:http://www.cryptosmith.com/archives/621]。我正在尝试使用密钥加密消息,但在获取密钥流后,我在执行xor数据时卡住了。我可能在几个地方犯了错误,因为我的结果显示为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是键。你能否指导我理解我的代码问题,请建议正确使用密文。

0 个答案:

没有答案