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 comentario

asmita kumari
asmita kumari el 27 de En. de 2022
I also want a in MATLAB on topic color image encryption and decryption using RSA algorithm

Iniciar sesión para comentar.

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 22 de Sept. de 2014

0 votos

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 comentarios

Anisha Coelho
Anisha Coelho el 23 de Sept. de 2014
Geoff Hayes - Thank you for your quick response.
I have understood the errors in my code and fixed them. The code works perfectly now.
The decrypted image has a few flaws. Can u suggest any way in which I can rectify them?
Geoff Hayes
Geoff Hayes el 23 de Sept. de 2014
Anisha - what are the flaws? How are you doing the decryption?
Anisha Coelho
Anisha Coelho el 23 de Sept. de 2014
I'm sorry. My bad. The value of modulus was less than 255. For higher values i got back the original image.
Thanks again
gopal varshney
gopal varshney el 6 de Nov. de 2015
the above program gives an error
gopal varshney
gopal varshney el 6 de Nov. de 2015
??? Error: File: rsa2.m Line: 11 Column: 1 Function definitions are not permitted at the prompt or in scripts. this is the error
The code
function en = i_encrypt(in,n,e)
en=1;
for i=e:-1:1
en=rem(en*in,n);
end
needs to be saved into a file named i_encrypt.m and cannot be in the same file as the rest of the code from the original posting.
cathrin philo
cathrin philo el 3 de Feb. de 2016
the above program gives me the error It is coming as : Undefined function or variable 'n'.
Error in ==> enImg(i)= i_encrypt(inImg(i),n,e);
Walter Roberson
Walter Roberson el 3 de Feb. de 2016
Notice the bottom of the original posting,
"Here n is the modulus, e is the key"
That is, you need to have assigned values to those two before you can use this code.
user00555 00
user00555 00 el 16 de Mzo. de 2016
Editada: Geoff Hayes el 16 de Mzo. de 2016
Using the above code encryption was successful. But decryption is not working. The code I've used is as follows, but maybe i'm missing something. Please help me.
% Decryption:
deImg = ones(s);
for i = 1:s
deImg(i)= i_decrypt(enImg(i),n,d);
end
% Function:
function de = i_decrypt(en,n,d)
de=1;
for i=d:-1:1
de=rem(de*in,n);
end
figure;imshow(uint8(deImg);
Geoff Hayes
Geoff Hayes el 16 de Mzo. de 2016
user0055500 - s is an array (or vector) telling you the size of the encrypted image. In order to iterate over each pixel you will need to do something like
for u = 1:s(1)
for v=1:s(2)
deImg(u,v) = i_decrypt(enImg(u,v),n,d);
end
end
Use the debugger to step through the code and you will be more likely able to determine what is wrong with the code.
Aayush Sinha
Aayush Sinha el 22 de Mzo. de 2016
Can you please provide the whole decryption code ? Tried doing the decryption but gives error. Thank you in advance
Image Analyst
Image Analyst el 22 de Mzo. de 2016
Posting cryptography code would not be allowed by the Mathworks. They'd take it down if they found it. It could even be illegal.
iron man
iron man el 28 de Jul. de 2016
whats the decryption function code? pls hlp
Geoff Hayes
Geoff Hayes el 28 de Jul. de 2016
See previous comment.
iron man
iron man el 28 de Jul. de 2016
In watermarking using RSA,how to apply rsa to get the same values we encrypted, when encrypted values change after applying attack on watermarked image?
Walter Roberson
Walter Roberson el 28 de Jul. de 2016
We cannot talk about RSA implementation or theory here due to the laws of the USA, other than to acknowledge that it exists.
iron man
iron man el 30 de Jul. de 2016
@walter roberson....sir please ,could u give me any tips on this ..please email me at imironman935@gmail.com
Walter Roberson
Walter Roberson el 31 de Jul. de 2016
iron man:
No. I do not live in the USA, but I live in a country that is bound by the same treaty that considers encryption to be a weapon of war. You would pretty much need to be in my physical presence for me to be permitted to assist you with RSA.
Sanjeeb Behera
Sanjeeb Behera el 13 de Sept. de 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 el 13 de Sept. de 2016
I got it but wnen i input n and e value it doesn't give any output

Iniciar sesión para comentar.

Más respuestas (1)

PRAVIN M
PRAVIN M el 1 de Mzo. de 2017

0 votos

image encryption how to decrypt the image for the same code

3 comentarios

Walter Roberson
Walter Roberson el 1 de Mzo. de 2017
Sorry, we cannot discuss that any more than we already have. I think we might have gone too far already.
Husna Ariffin
Husna Ariffin el 2 de Oct. de 2017
Editada: Walter Roberson el 2 de Oct. de 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 el 2 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 22 de Sept. de 2014

Comentada:

el 27 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by