-
Notifications
You must be signed in to change notification settings - Fork 4
/
bEncrypt.m
25 lines (23 loc) · 850 Bytes
/
bEncrypt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function eOut=bEncrypt(bIn, key, dir)
% Encrypts a string of bits. Flips the bits around a certain number of
% times given by the value of each entry on key and the number of entries
% in key.
% Error checking
if(abs(dir)~=1)
fprintf('ERROR: dir in bEncrypt can only be 1 or -1.\n');
return
end
% Length of the key and the input.
bLen=length(bIn);
kLen=length(key);
% If dir is 1, count up. If -1, count down.
for i=0.5*(kLen+1+dir*(1-kLen)):dir:0.5*(kLen+1+dir*(kLen-1))
for j=0.5*(bLen+1+dir*(1-bLen)):dir:0.5*(bLen+1+dir*(bLen-1))
n=mod(j+key(i)-1,bLen)+1; % Select the bit to switch with j.
temp=bIn(j); % store a temprary value.
bIn(j)=bIn(n);
bIn(n)=temp;
end
end
eOut=bIn; % output the result
end