Reputation: 61
I performed color image permutation using 2D sine-map for RGB image. The code uses this map to perform pixel location permutation for each RGB channel in the image. Then, I used the same map with the same initial parameters to inverse the permutation. The problem is the code only perform correct inverse permutation for one channel only the other channels is not recovered. See the attached images of the permuted and the recovered image.
%% Image Encryption Demo - Encryption and Decryption
clear all
close all
clc
%% 1. Load plaintext images
% Image 1
I = imread('D:\1\1.jpg');
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
K=1;
%% 2. Encryption
%[CI,K] = Logistic2D_ImageCipher(I,'encryption');
[CIb,K] = Logistic2D_ImageCipher(Ib,'encryption');
clearvars -except CIb Ir Ig Ib K I;
[CIr,K] = Logistic2D_ImageCipher(Ir,'encryption');
clearvars -except CIb CIr Ir Ig Ib K I;
[CIg,K] = Logistic2D_ImageCipher(Ig,'encryption');
clearvars -except CIb CIr CIg Ir Ig Ib K I;
CI = cat(3,CIr,CIg,CIb);
%% 3. Decryption
%DI = Logistic2D_ImageCipher(CI,'decryption',K);
DIb = Logistic2D_ImageCipher(CIb,'decryption',K);
clearvars -except CIb CIr CIg Ir Ig Ib K I DIb CI;
DIg = Logistic2D_ImageCipher(CIg,'decryption',K);
clearvars -except CIb CIr CIg Ir Ig Ib K I DIb DIg CI;
DIr = Logistic2D_ImageCipher(CIr,'decryption',K);
clearvars -except CIb CIr CIg Ir Ig Ib K I DIb DIg DIr CI;
DI = cat(3,DIr,DIg,DIb);
%% 4. Analaysis
% Histogram
%title('aaa');
figure,subplot(221),imshow(I,[]),subplot(222),imshow(CI,[])
subplot(223),imhist(I),subplot(224),imhist(CI)
title('aaa2');
figure,subplot(221),imshow(DI,[])
function varargout = Logistic2D_ImageCipher(P,para,K)
%% 1. Initialization
% 1.1. Genereate Random Key if K is not given
if ~exist('K','var') && strcmp(para,'encryption')
K = round(rand(1,256));
varOutN = 2;
elseif ~exist('K','var') && strcmp(para,'decryption')
error('Cannot Complete Decryption without Encryption Key')
else
varOutN = 1;
end
% 1.2. Translate K to map formats
transFrac = @(K,st,ed) sum(K(st:ed).*2.^(-(1:(ed-st+1))));
x0 = transFrac(K,1,52);
y0 = transFrac(K,53,104);
a=0.8;
b =0.3;
r = transFrac(K,105,156)*.08+1.11;
T = transFrac(K,157,208);
turb = blkproc(K(209:256),[1,8],@(x) bi2de(x));
MN = numel(P);
Logistic2D = @(x,y,a) [sin(pi*a*(y+3)*x*(1-x)), sin(pi*a*(x+3)*y*(1-y))];
format long eng
%% 2. Estimate cipher rounds
if max(P(:))>1
F = 256;
S = 4;
else
F = 2;
S = 32;
end
P = double(P);
iter = 1;
%iter = ceil(log2(numel(P))/log2(S));
%% 3. Image Cipher
C = double(P);
switch para
case 'encryption'
for i = 1:iter
tx0 = mod(log(turb(mod(i-1,6)+1)+i)*x0+T,1);
ty0 = mod(log(turb(mod(i-1,6)+1)+i)*y0+T,1);
xy = zeros(MN,2);
for n = 1:MN
if n == 1
xy(n,:) = (Logistic2D(tx0,ty0,a));
else
xy(n,:) = (Logistic2D(xy(n-1,1),xy(n-1,2),a));
end
end
R = cat(3,reshape(xy(:,1),size(P,1),size(P,2)),reshape(xy(:,2),size(P,1),size(P,2)));
C = LogisticPermutation(C,R,'encryption');
end
case 'decryption'
for i = iter:-1:1
tx0 = mod(log(turb(mod(i-1,6)+1)+i)*x0+T,1);
ty0 = mod(log(turb(mod(i-1,6)+1)+i)*y0+T,1);
xy = zeros(MN,2);
for n = 1:MN
if n == 1
xy(n,:) = (Logistic2D(tx0,ty0,a));
else
xy(n,:) = (Logistic2D(xy(n-1,1),xy(n-1,2),a));
end
end
R = cat(3,reshape(xy(:,1),size(P,1),size(P,2)),reshape(xy(:,2),size(P,1),size(P,2)));
C = LogisticPermutation(C,R,'decryption');
end
end
%% 4. Output
switch F
case 2
C = logical(C);
case 256
C = uint8(C);
end
switch varOutN
case 1
varargout{1} = C;
case 2
varargout{1} = C;
varargout{2} = K;
end
function C = LogisticPermutation(P,R,para)
C0 = zeros(size(P));
C = C0;
switch para
case 'encryption'
% 1. Shuffling within a Column
[v,Epix] = sort(R(:,:,1),1);
for i = 1:size(R,1)
C0(:,i) = P(Epix(:,i),i);
end
% 2. Shuffling within a Row
[v,Epiy] = sort(R(:,:,2),2);
for j = 1:size(R,2)
C(j,:) = C0(j,Epiy(j,:));
end
case 'decryption'
% 1. Shuffling within a Row
[v,Epiy] = sort(R(:,:,2),2);
for j = 1:size(R,2)
C0(j,Epiy(j,:)) = P(j,:);
end
% 2. Shuffling within a Column
[v,Epix] = sort(R(:,:,1),1);
for i = 1:size(R,1)
C(Epix(:,i),i) = C0(:,i);
end
end
Upvotes: 1
Views: 268
Reputation: 24179
It didn't work because you overwrote K
in the encryption stage, and it was different for each channel, so the decryption only worked correctly for the last channel that created K
. If you're using the MATLAB editor, you should pay attention to the mlint warnings (square at the top right that should be always be green) - you could say that this is what told me the answer to your problem.
Here's a fixed version of the first part of the script:
function q50823167
%% 1. Load plaintext images
% Image 1
I = imread(fullfile(matlabroot, 'examples', 'wavelet', 'mandrill.jpg'));
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
%% 2. Encryption
%[CI,K] = Logistic2D_ImageCipher(I,'encryption');
[CIb,Kb] = Logistic2D_ImageCipher(Ib,'encryption');
[CIr,Kr] = Logistic2D_ImageCipher(Ir,'encryption');
[CIg,Kg] = Logistic2D_ImageCipher(Ig,'encryption');
CI = cat(3,CIr,CIg,CIb);
%% 3. Decryption
%DI = Logistic2D_ImageCipher(CI,'decryption',K);
DIb = Logistic2D_ImageCipher(CIb,'decryption',Kb);
DIg = Logistic2D_ImageCipher(CIg,'decryption',Kg);
DIr = Logistic2D_ImageCipher(CIr,'decryption',Kr);
DI = cat(3,DIr,DIg,DIb);
%% 4. Analaysis
% Histogram
%title('aaa');
figure,subplot(221),imshow(I,[]),subplot(222),imshow(CI,[])
subplot(223),imhist(I),subplot(224),imhist(CI)
title('aaa2');
figure,subplot(221),imshow(DI,[])
Note that you don't have to clear variables every time.
Upvotes: 2