Image Encryption using RSA

28 views (last 30 days)
Anisha Coelho
Anisha Coelho on 22 Sep 2014
Commented: asmita kumari on 27 Jan 2022
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.
  1 Comment
asmita kumari
asmita kumari on 27 Jan 2022
I also want a in MATLAB on topic color image encryption and decryption using RSA algorithm

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 22 Sep 2014
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.
  20 Comments
Sanjeeb Behera
Sanjeeb Behera on 13 Sep 2016
Above implementation would not work. It gives an error in Error in ==> imgrsa at 7 enImg(i,j)= i_encrypt(inImg(i,j),n,e);
Sanjeeb Behera
Sanjeeb Behera on 13 Sep 2016
I got it but wnen i input n and e value it doesn't give any output

Sign in to comment.

More Answers (1)

PRAVIN M
PRAVIN M on 1 Mar 2017
image encryption how to decrypt the image for the same code
  3 Comments
Husna Ariffin
Husna Ariffin on 2 Oct 2017
Edited: Walter Roberson on 2 Oct 2017
How to do the decryption? I have tried but I got blue dot image (Still encrypt). Correct me please... Below is my code :
[Code removed]
Walter Roberson
Walter Roberson on 2 Oct 2017
Husna Ariffin:
As of 2009, non-military cryptography exports from the U.S. are controlled by the Department of Commerce's Bureau of Industry and Security. Some restrictions still exist, even for mass market products, particularly with regard to export to "rogue states" and terrorist organizations. Militarized encryption equipment, TEMPEST-approved electronics, custom cryptographic software, and even cryptographic consulting services still require an export license (pp. 6–7). Furthermore, encryption registration with the BIS is required for the export of "mass market encryption commodities, software and components with encryption exceeding 64 bits" (75 FR 36494). In addition, other items require a one-time review by, or notification to, BIS prior to export to most countries. For instance, the BIS must be notified before open-source cryptographic software is made publicly available on the Internet, though no review is required. Export regulations have been relaxed from pre-1996 standards, but are still complex. Other countries, notably those participating in the Wassenaar Arrangement, have similar restrictions." (emphasis added)
Mathworks cannot provide that advanced notification to the Bureau of Industry and Security (BIS), and therefor it is illegal for Mathworks to host discussion of encryption code or techniques. We literally cannot talk about it here.
You will need to find a different resource to discuss your encryption code.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!