You are on page 1of 11

1.

Matlab Image Encryption Code


(URL: http://www.cheers4all.com/2012/04/matlab-image-encryption-code/) This project is Image Encryption & Decryption. The user will give an input and encryption factor. The image will be converted to an encrypted image file. This image is not understandable by any one. 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

function ImageEncryptionGui_OpeningFcn(hObject, eventdata, handles, varargin) handles.output = hObject; axes(handles.axes4) %BackGr = imread('2redpanda.jpg'); %imshow(BackGr);

guidata(hObject, handles); clear all; clc; global Img; global EncImg; global DecImg;

function varargout = ImageEncryptionGui_OutputFcn(hObject, eventdata, handles)

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);

function pushbutton3_Callback(hObject, eventdata, handles) global DecImg; global EncImg; global key; DecImg = imageProcess(EncImg,key); axes(handles.axes3); imshow(DecImg); imwrite(DecImg,'Decoded.jpg','jpg'); guidata(hObject, handles);

Image Process Code:


function [proImageOut] = imageProcess(ImgInp,key) [n m k] = size(ImgInp); % key =cell2mat(struct2cell( load('key5.mat'))); % key = keyGen(n*m); for ind = 1 : m Fkey(:,ind) = key((1+(ind-1)*n) : (((ind-1)*n)+n)); end len = n; bre = m; for ind = 1 : k Img = ImgInp(:,:,ind); for ind1 = 1 : len for ind2 = 1 : bre proImage(ind1,ind2) = bitxor(Img(ind1,ind2),Fkey(ind1,ind2)); end end proImageOut(:,:,ind) = proImage(:,:,1); end % figure,imshow(proImageOut); return;

2. Selective Encryption Matlab Code (URL: http://selectiveimageencryption.blogspot.in/2012/07/selectiveencryption-matlab-code.html)

In an image encryption algorithm, images are protected by using encryption techniques, called fully layered; in this approach the

whole content of the images is encrypted. But in the case of selective encryption, some content of the image is unencrypted. Selective encryption algorithm reduces the execution time because it encrypts only a part of the image. Consequently, selective encryption is sometimes called partial encryption. The matlab code for selective encryption algorithm is as follows: % Program for Selective Encryption function selective_encryption tic [i11,i12]=uigetfile('*.*');%XXXXXX Input Image XXXXXXXXXXX i12=strcat(i12,i11); im2=imread(i12); % im12=imread('1.jpg'); im=imresize(im2,[400 400]); im1=im(:,:,1);

z=input('Select the key for Image Encryption ') ; z1=z; % im1=rgb2gray(im); % im1=medfilt2(im1,[3 3]); % figure(1); subplot(2,2,1);imshow(im); title('Input Image'); im2=double(im1); [M,N]=size(im2); e=hundungen(M,N,0.1); tt=0.001;

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.1 (a)Input Image (b)Selective Encrypted Image (c)Decrypted Image

Fig.2 (a)Histogram of the Original Image (b)Histogram of the Selectively Encrypted Image (c)Histogram of Output Image

Hence, fig.1 and fig.2 suggest that the algorithm is efficient to encrypt and decrypt the image.
Please check the following links: http://frankzhan.com/blog/2012/04/01/simple-matlab-code-encrypting-2d-image-usinghyperbolic-equation/ http://www.fileguru.com/apps/matlab_codes_for_image_encryption http://www.freedownloadmanager.org/download/code-image-encryption-using-matlab4974651.html

You might also like