loop for image encryption gives error
Show older comments
I am using 8*8 matrix for image encryption but there is an error in loop which i cannot figure out why.
Km=[K11 K12;K21 K22];% 8 cross 8 matrix
I=imread('lena256.jpg');
imshow(I);
[X Y]=size(I);
for i=1:X
for j=1:4:Y
P=double(I(i,j:j+3));
EncImg(i,j:j+3)=mod((Km*P')',256);
end
error is in EncImg(i,j:j+3)=mod((Km*P')',256))
7 Comments
sadiqa ilyas
on 31 Jul 2019
darova
on 31 Jul 2019
index exceeds matrix dimensions
for j=1:4:Y % when j == Y
P=double(I(i,j:j+3)); % you are trying to get access to I(i,Y:Y+3)
Walter Roberson
on 31 Jul 2019
I suspect that your image is rgb and that you are misusing size()
sadiqa ilyas
on 31 Jul 2019
sadiqa ilyas
on 31 Jul 2019
darova
on 31 Jul 2019
What did you change to get rid of it?
Walter Roberson
on 31 Jul 2019
Most jpg images are rgb even when they look gray. Real grayscale jpg images are rare. You should always check ndims of a jpg image and rgb2gray if it is not 2.
Accepted Answer
More Answers (1)
sadiqa ilyas
on 1 Aug 2019
4 Comments
Walter Roberson
on 1 Aug 2019
I1 is a 3D array because it is RGB. You are passing it to SBox(). We do not know what SBox does with it, but likely SBox is also returning a 3D array (RGB). Then you take size() in exactly the way I told you twice before was wrong.
[X, Y]=size(I2);
will not, I repeat not return the number of rows into X, and the number of columns into Y when I2 is an RGB array: it would instead return the number of rows into X, and the number of color panes (i.e., 3) times the number of columns into Y.
You then proceed to try to encrypt the RGB image into a 2D array instead of encrypting it into a 3D array as would be needed for RGB.
sadiqa ilyas
on 1 Aug 2019
Walter Roberson
on 1 Aug 2019
This is getting close to the point where I have to stop you and say that we cannot help you any further. Due to the laws of the USA, we can effectively only discuss encryption here as long as your program is pretty broken, to the point where someone else would have trouble using the code to implement a working encryption function. When we start getting to the point where there is a chance that your program might start working, we cannot assist you further and we cannot permit you to post code that is nearly working.
The laws of the USA about this might not be wise or convenient, but we have to live with them.
sadiqa ilyas
on 1 Aug 2019
Categories
Find more on Texture Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

