loop for image encryption gives error

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

lena image is of size 256 *256
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)
I suspect that your image is rgb and that you are misusing size()
its a gray scale image
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)
problem is same it again gives idex exceeds matrix dimensions
What did you change to get rid of it?
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.

Sign in to comment.

 Accepted Answer

for j=1:4:Y-3

12 Comments

for j=1:4:Y-3
it gives error in the next line.
Please do share K11 K12;K21 K22 and lena256 image.
You are trying to multiply Km*P', where Km is 8x8 and P is 4x1?
Are you sure there is no dimention issue?
Thanks I have solved the problem for gray scale by taking j=1:8:Y and it works. Now problem is when i encrypt 3 rgb fig.
K11=144 234 121 183;82 16 111 231;62 195 73 188;107 164 119 213]
K12=113 22 135 73;174 238 145 25;194 61 184 68;149 92 137 44
K21=114 22 135 73;174 239 145 25;194 61 185 68;149 92 137 45
K22=144 234 121 183;82 19 111 231;62 195 73 188;107 164 119 213
Km=[K11 K12;K21 K22]
I1=imread('Baboon.jpg');
h=size(I1);
figure;imshow(I1,[])
for z=1:3
for i=1:h(1)
for j=1:8:h(2)
P=double(EncImg(i,j:j+7,z));
EncImg2(i,j:j+7,z)=mod((Km*P')',256);
end
end
end
instead of encrypted images it gives a blank page or screen
what is EncImg?
encrypted image
Do you have separate image as EncImg, as you defined here?
P=double(EncImg(i,j:j+7,z));
if yes, do share
that is orignal 3 dim image
If i am using the statements shared below , Y coordinate gets bigger that is more than 256 which i donot understand why.
EncImg=zeros(X,Y);
for i=1:X
for j=1:8:Y-7
P=double(I2(i,j:j+7));
EncImg(i,j:j+7)=mod((Km*P')',256);
end
You are continously changing the code.
Initialy
P=double(EncImg(i,j:j+7,z));
Now
P=double(I2(i,j:j+7));
Now another term I2, Have you defined I2? Be ensure that all variables or images must be defined before it used.
It would be better, if you provide the complete code, what you have, all inputs required (images also), the code you have tried so far and what result you are expecting.
When you use
[X Y]=size(I);
then that acts like
temp = size(I);
X = temp(1);
Y = prod(temp(2:end));
For RGB files, that results in Y being 3 times the number of columns instead of being the number of columns.

Sign in to comment.

More Answers (1)

As i m also trying to do some thing with the code. here is the complete code.It gives me an encrypted image but y size is doubled and i am not getting 256 cross 256 enciphered image. I have applied s box as well.peppers1.bmp
11=mod([144 234 121 183;82 275 111 231; 62 195 73 188;107 164 119 213],256);
K12=mod(eye(4,4)-K11,256);
K21=mod(eye(4,4)+K11,256);
K22=mod(-K11,256);
Km=[K11 K12;K21 K22];
I1=imread('peppers.bmp');
%I1=rgb2gray(I1);
%I1=I1(:,:,1);figure;
imshow(I1,[]);%title('red');
%I1=I1(:,:,2);figure;%title('green')
%I1=I1(:,:,3);figure;%title('blue')
imshow(I1,[])
imsave
imhist(I1)
I2=uint8(SBox(I1));
[X,Y]=size(I2);
EncImg=zeros(X,Y);
for i=1:X
for j=1:8:Y
P=double(I2(i,j:j+7));
EncImg(i,j:j+7)=mod((Km*P')',256);
end
end

4 Comments

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.
yes you r right . I have changed the command and it gives better encrypted imged. but not the one i wanted imagt.Why these black lines occur in the encrypted image.
I1=I1(:,:,1); figure
the image I got is1.png
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!