www.pudn.com > encryption.rar > encryption.m
function encryption(string1, string2, x0, y0)
%ENCRYPTION encrypt the input image
if nargin < 2
error('not enough input arguments.');
end
if nargin < 3 | isempty(x0)
x0 = 0.10384;
else
if sum(size(x0)) > 2
error('x0 must be scalar.');
end
end
if nargin < 4 | isempty(y0)
y0 = 0.80637;
else
if sum(size(y0)) > 2
error('y0 must be scalar.');
end
end
img1 = imread(string1);
img2 = imread(string2);
S1 = size(img1);
S2 = size(img2);
if length(S1) ~= 3 | length(S2) ~= 3 | S1(1) ~= S2(1) | S1(2) ~= S2(2) | S1(3) ~= S2(3)
error('the two input image should be the same size M x N x 3.');
end
figure;
subplot(2,2,1);
imshow(img1,[]);
title('A image');
subplot(2,2,2);
imshow(img2,[]);
title('B image');
R = S1(1);
C = S1(2);
r = mod(S1(1), 8);
c = mod(S1(2), 8);
%padding
if r ~=0 || c ~=0
img1 = padarray(img1, [(8-r) (8-c)], 'post');
img2 = padarray(img2, [(8-r) (8-c)], 'post');
end
S3 = size(img1);
num = S3(1)*S3(2);
X = zeros(num, 1);
Y = zeros(num, 1);
X(1) = x0;
Y(1) = y0;
%cut the beginning digital
for i = 1:100
X(1) = 1 - 2*X(1).^2;
Y(1) = 1 - 2*Y(1).^2;
end
%logistic serials
for i = 2:num
X(i) = 1 - 2*X(i-1).^2;
Y(i) = 1 - 2*Y(i-1).^2;
end
%chaotic matrix
[X1 x1] = sort(X);
X2(x1) = 1:num;
X3 = reshape(X2, S3(1), S3(2));
%symbol matrix
Y1 = zeros(num, 1);
Y2 = Y1 <= Y;
Y3 = reshape(Y2, S3(1), S3(2));
%encryption in space domain
% permutation of A
for i = 1:S3(1)
for j = 1:S3(2)
l = mod(X3(i, j), S3(2));
if l == 0
l = S3(2);
end
k = (X3(i, j) - l) / S3(2) + 1;
temp = img1(i, j, :);
if X3(i, j) >= num / 2
img1(i, j, :) = img2(k, l, :);
img2(k, l, :) = temp;
else
img1(i, j, :) = img1(k, l, :);
img1(k, l, :) = temp;
end
end
end
%permutation of B
for i = 1:S3(1)
for j = 1:S3(2)
l = mod(X3(i, j), S3(2));
if l == 0
l = S3(2);
end
k = (X3(i, j) - l) / S3(2) + 1;
temp = img2(i, j, :);
if X3(i, j) >= num / 2
img2(i, j, :) = img1(k, l, :);
img1(k, l, :) = temp;
else
img2(i, j, :) = img2(k, l, :);
img2(k, l, :) = temp;
end
end
end
% %encryption in DCT domain
% img1 = double(img1) / 255; %important
% img2 = double(img2) / 255;
%
% %DCT
% for k = 1:S3(1)/8
% p = (k-1)*8+1;
% for l = 1:S3(2)/8
% q = (l-1)*8+1;
% temp1 = zeros(8, 8, 3);
% temp2 = zeros(8, 8, 3);
% for u = 0:7
% for v = 0:7
% if u == 0
% Cu = 1/sqrt(2);
% else
% Cu = 1;
% end
%
% if v == 0
% Cv = 1/sqrt(2);
% else
% Cv = 1;
% end
%
% for i = 0:7
% for j = 0:7
% temp1(u + 1, v + 1, :) = temp1(u + 1, v + 1, :) + 0.25*Cu*Cv*img1(p+i, q+j, :)*cos(3.1415926*(2*i+1)*u/(2*8))*cos(3.1415926*(2*j+1)*v/(2*8));
% temp2(u + 1, v + 1, :) = temp2(u + 1, v + 1, :) + 0.25*Cu*Cv*img2(p+i, q+j, :)*cos(3.1415926*(2*i+1)*u/(2*8))*cos(3.1415926*(2*j+1)*v/(2*8));
% end
% end
% end
% end
% img1(p:(p+7), q:(q+7), :) = temp1;
% img2(p:(p+7), q:(q+7), :) = temp2;
% end
% end
% row of A
for i = 1:S3(1)
for j = 1:S3(2)
temp = img1(i, :, :);
if Y3(i, j) == 1
img1(i, :, :) = img2(i, :, :);
img2(i, :, :) = temp;
end
end
end
% column of B
for i = 1:S3(1)
for j = 1:S3(2)
temp = img2(:, j, :);
if Y3(i, j) == 1
img2(:, j, :) = img1(:, j, :);
img1(:, j, :) = temp;
end
end
end
% %IDCT
% for k = 1:S3(1)/8
% p = (k-1)*8+1;
% for l = 1:S3(2)/8
% q = (l-1)*8+1;
% temp1 = zeros(8, 8, 3);
% temp2 = zeros(8, 8, 3);
% for i = 0:7
% for j = 0:7
% for u = 0:7
% for v = 0:7
% if u == 0
% Cu = 1/sqrt(2);
% else
% Cu = 1;
% end
%
% if v == 0
% Cv = 1/sqrt(2);
% else
% Cv = 1;
% end
% temp1(i + 1, j + 1, :) = temp1(i + 1, j + 1, :) + 0.25*Cu*Cv*img1(p+u, q+v, :)*cos(3.1415926*(2*i+1)*u/(2*8))*cos(3.1415926*(2*j+1)*v/(2*8));
% temp2(i + 1, j + 1, :) = temp2(i + 1, j + 1, :) + 0.25*Cu*Cv*img2(p+u, q+v, :)*cos(3.1415926*(2*i+1)*u/(2*8))*cos(3.1415926*(2*j+1)*v/(2*8));
% end
% end
% end
% end
% img1(p:(p+7), q:(q+7), :) = temp1;
% img2(p:(p+7), q:(q+7), :) = temp2;
% end
% end
%
% img1 = im2uint8(img1); %important
% img2 = im2uint8(img2);
subplot(2,2,3);
imshow(img1,[]);
title('encrypted image of A');
imwrite(img1, 'encrypted-A.jpg');
subplot(2,2,4);
imshow(img2,[]);
title('encrypted image of B');
imwrite(img2, 'encrypted-B.jpg');