MATLAB: Image Encryption using RSA

encryptionimage processingMATLABpixelsrsa

I am trying to encrypt an image using RSA algorithm
inImg = imread('lena.jpg');
s = size(inImg);
% Encryption:
enImg = ones(s);
for i = 1:s
enImg(i)= i_encrypt(inImg(i),n,e);
end
% Function:
function en = i_encrypt(in,n,e)
en=1;
for i=e:-1:1
en=rem(en*in,n);
end
Here n is the modulus, e is the key
However after all these steps all the pixels have the value of 1. Even if the value isn't one, the displayed image is an all white image. Can u please tel me where I have gone wrong.

Best Answer

Anisha - there may be two problems with the above code. The first is the iterating over the pixels of the input image, in this case lena.jpg. Is this a 2D (grayscale) or 3D (RGB) image? Because this affects how you go about encrypting the pixels. Let us assume that the image is grayscale and so is 2D with dimensions mxn. Then
s = size(inImg);
returns a vector like [m n] where m is the number of rows in the image and n is the number of columns. Initializing the encrypted image as
enImg = ones(s);
is fine as enImg will be a mxn matrix (of type double). But consider how the code is iterating over the pixels
for i = 1:s
where s is a vector. If you step through this, you will notice that i iterates from 1 to m (the first element in the vector s) so only the first column of the image is encrypted. It needs to iterate over every element in the image as either
for u=1:s(1) % iterate over each row
for v=1:s(2) % iterate over each column
enImg(u,v) = i_encrypt(inImg(u,v),n,e);
end
end
Of course, then the code has to be adjusted for RGB images with their third dimension. So it may be more convenient to do the following
for u=1:numel(inImg)
enImg(u) = i_encrypt(inImg(u),n,e);
end
where we iterate over each element of the image regardless as to whether the image is 2D or 3D.
The second problem involves the data type of the input image. Presumably it is uint8. The encryption will multiply the input pixel in by itself for e times, applying the modulo operation at each step.
Consider the following by running it in the Command Window
pix = uint8(2);
for k=1:100
pix=pix*2;
fprintf('k=%d %d\n',k,pix);
end
We multiply the 8-bit unsigned integer 2 by itself for 100 times. Observe that at iteration 7, the result is 255 (which is odd!) and remains at that value for the next 90+ iterations. This is because the maximum value for the uint8 data type is 255. Depending upon your choice of the key, each pixel in your encrypted image may have 255 assigned to it (assuming that your modulus n is greater than 255) and so if you attempt to display the encrypted image (cast as 8-bit unsigned integers) then the image will be white.
What you should do instead is to cast the input image as a double, and then do the encryption.
inImg = double(imread('lena.jpg'));
s = size(inImg);
enImg = ones(s);
for u=1:numel(inImg)
enImg(u) = i_encrypt(inImg(u),n,e);
end
The encrypted image, enImg, will (most likely) have elements greater than 255 (and be of data type double).
Decrypt enImg and cast back to the uint8 data type, and display it to get the original image.
Related Question