Professional Documents
Culture Documents
Matlab Image Encryption Code
Matlab Image Encryption Code
When the receiver will receive the encrypted file he will decrypt it so he will get the original file.
We have used a simple GUI for our cryptosystem. We have used three push buttons in our GUI
representing
1. Input Image Select
2. Encrypt Image
3. Decrypt Image
Main Code:
function varargout = ImageEncryptionGui(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ImageEncryptionGui_OpeningFcn, ...
'gui_OutputFcn', @ImageEncryptionGui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
guidata(hObject, handles);
clear all;
clc;
global Img;
global EncImg;
global DecImg;
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
global Img;
global key;
X = uigetfile('*.jpg;*.tiff;*.ppm;*.pgm;*.png','pick a jpge file');
Img = imread(X);
axes(handles.axes1)
imshow(Img);
[n m k] = size(Img);
key = keyGen(n*m);
guidata(hObject, handles);
function pushbutton2_Callback(hObject, eventdata, handles)
global Img ;
global EncImg;
global key;
EncImg = imageProcess(Img,key);
axes(handles.axes2)
imshow(EncImg);
imwrite(EncImg,'Encoded.jpg','jpg');
guidata(hObject, handles);
im3=mod(tt*im2+(1-tt)*e,256);
a1(:,:,1)=round(im3(:,:,1));a1(:,:,2)=im(:,:,2);a1(:,:,3)=im(:,:,3);
for i=1:400
for j=1:400
a1(i,j,1)=bitxor (a1(i,j,3),a1(i,j,1));
a1(i,j,2)=bitxor (a1(i,j,1),a1(i,j,2));
a1(i,j,3)=bitxor (a1(i,j,2),a1(i,j,3));
end
end
for i=1:400
for j=1:400
a1(i,j,1)=bitxor (a1(i,j,3),a1(i,j,2));
a1(i,j,2)=bitxor (a1(i,j,1),a1(i,j,3));
a1(i,j,3)=bitxor (a1(i,j,2),a1(i,j,1));
end
end
% for i=1:400
% for j=1:400
%
a1(i,j,1)=mod((a1(i,j,3)+a1(i,j,1)),255);
%
a1(i,j,2)=mod((a1(i,j,1)+a1(i,j,2)),255);
%
a1(i,j,3)=mod((a1(i,j,2)+a1(i,j,3)),255);
%%
% end
% end
% for i=1:400
% for j=1:400
%
a1(i,j,1)=mod((a1(i,j,2)+a1(i,j,3)),255);
%
a1(i,j,2)=mod((a1(i,j,3)+a1(i,j,1)),255);
%
a1(i,j,3)=mod((a1(i,j,1)+a1(i,j,2)),255);
%%
% end
% end
% figure(2);
subplot(2,2,2);imshow(uint8(a1),[]);
title('Encrypted Image');
toc
clc;
% ----------------------------------------------------------------if (z1==input('Enter the key '))
tic
e=keygen(M,N,0.1);
im5=(im3-(1-tt)*e)/tt;
a1(:,:,1)=round(im5(:,:,1));a1(:,:,2)=im(:,:,2);a1(:,:,3)=im(:,:,3);
% for i=1:400
% for j=1:400
%
a1(i,j,1)=bitxor (a1(i,j,3),a1(i,j,1));
%
a1(i,j,2)=bitxor (a1(i,j,1),a1(i,j,2));
%
a1(i,j,3)=bitxor (a1(i,j,2),a1(i,j,3));
%
% end
% end
% figure(3);
subplot(2,2,3);imshow(uint8(a1),[]);
title('Final Image');
figure(4);
subplot(231)
imhist(uint8(im1));
title('Histogram of input image');
subplot(233)
imhist(uint8(im3));
title('Histogram of Selectively Encrypted image');
subplot(234)
imhist(uint8(im5));
title('Histogram of final Image');
else
input('************** Sorry,Wrong Key
*******************! ');
end
toc
____________________________________________________
__
function e=hundungen(M,N,key0)
for(i=1:200)
key0=3.925*key0*(1-key0);
end
key1=3.925;
for(i=1:M)
for(j=1:N)
key0=key1*key0*(1-key0);
a(i,j)=key0;
end
end
key3=0.2;
key2=3.925;
for(i=1:M)
for(j=1:N)
key3=key2*key3*(1-key3);
b(i,j)=key3;
end
end
key4=0.3;
key2=3.925;
for(i=1:M)
for(j=1:N)
key4=key2*key4*(1-key4);
c(i,j)=key4;
end
end
t=0.4;
w0=0.2;
w1=0.5;
w2=0.3;
w=(1-t)^2*w0+2*t*(1-t)*w1+t^2*w2;
for(i=1:M)
for(j=1:N)
P(i,j)=(1-t)^2*a(i,j)*w0+2*t*(1t)*b(i,j)*w1+t^2*c(i,j)*w2;
% d(i,j)=P(i,j)/w;
d(i,j)=P(i,j);
end
end
x=d;
e=round(x*255);
end
____________________________________________________
__
function e=keygen(M,N,key0)
for(i=1:200)
key0=3.925*key0*(1-key0);
end
key1=3.925;
for(i=1:M)
for(j=1:N)
key0=key1*key0*(1-key0);
a(i,j)=key0;
end
end
key3=0.2;
key2=3.925;
for(i=1:M)
for(j=1:N)
key3=key2*key3*(1-key3);
b(i,j)=key3;
end
end
key4=0.3;
key2=3.925;
for(i=1:M)
for(j=1:N)
key4=key2*key4*(1-key4);
c(i,j)=key4;
end
end
t=0.4;
w0=0.2;
w1=0.5;
w2=0.3;
w=(1-t)^2*w0+2*t*(1-t)*w1+t^2*w2;
for(i=1:M)
for(j=1:N)
P(i,j)=(1-t)^2*a(i,j)*w0+2*t*(1t)*b(i,j)*w1+t^2*c(i,j)*w2;
% d(i,j)=P(i,j)/w;
d(i,j)=P(i,j);
end
end
x=d;
e=round(x*255);
end
%Note:- Copy the code,selective encryption code, keygen code
and hundungen function code, make seperate matlab file. Make
three 'm' file in matlab.One for selective encryption, second for
hundungen(M,N,key0) function and third for key generation.
Save the file with file name selective _encryption. Execute the
selective _encryption file and make sure both the files are in the
current directory at the time of execution. Key will be numeric.
____________________________________________________
____
Results
for
Lena
image
is
the
the
input
given
for
the
algorithm:
algorithm.
Fig.2 (a)Histogram of the Original Image (b)Histogram of the Selectively Encrypted Image
(c)Histogram of Output Image