Error using bitxor Inputs must have the same size.

Hello there,
i have a proplem with bitxor function :
Error using bitxor Inputs must have the same size.
the first input " original " & the second input " cipher " has the following sizes like below :
workspace variables
How can i make original & cipher variable of same size ?
and here is my code :
%Using Logistic chaotic mapping, sequence encryption of grayscale images
original = imread('pp.jpg');
figure(1);
subplot(1,3,1);
imshow(original);
title('original');
[M,N]=size(original);
x=0.1;
u=4;
% %Iterative200Second, achieve full chaotic state
for i=1:200
x=u*x*(1-x);
end
% %Generate a single-dimensional chaotic encryption sequence
A=zeros(1,M*N); %generate1*Mn zero matrix
A(1)=x;
%Generate chaotic sequence
for i=1:M*N-1
A(i+1)=u*A(i)*(1-A(i));
end
%Normalized sequence
B=uint8(255*A); %Convert to 255 type of data
% %Transforming into two-dimensional chaotic encryption sequence
Cipher=reshape(B,M,N); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encrypted =bitxor(original ,Cipher); %Tone or operation encryption
figure(1);
subplot(1,3,2);
imshow(Encrypted);
title('Encrypted');
%Decryption
Decrypted=bitxor(Encrypted,Cipher); %Different or operation decryption
figure(1);
subplot(1,3,3);
imshow(Decrypted);
title('Decrypted');
%Draw a histogram of original Image and Encrypted Image
figure(2);
subplot(1,3,1);
imhist(original);
title(' original ');
figure(2);
subplot(1,3,2);
imhist(Encrypted);
title('Encrypted');
figure(2);
subplot(1,3,3);
imhist(Decrypted);
title('Decrypted');
Thanks in advance.

 Accepted Answer

Jan
Jan on 11 Nov 2021
Edited: Jan on 11 Nov 2021
The mistake happens here:
[M,N]=size(original);
The original image is an RGB image, not a grey-scale image as the comments imply, which store its data in [nRows x nColumns x 3] array. With [M, N] = size(img) the N is nColumns*3.
Note:
[M, N] = size(X)
% is not the same as:
M = size(X, 1)
N = size(X, 2)
Better:
nByte = numel(original); % instead of: [M,N]=size(original);
... Modify code to use nByte instead of M*N
Cipher = reshape(B, size(original)); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encrypted = bitxor(original ,Cipher);

4 Comments

I appreciate your response MR. Jan
it worked, thanks
When I tried to implement your solution to my project this error appeared again
here is the entire code , Hope you can help me sir
THX in Advance
%NOMA images Logistic chaotic mapping sequence encryption
clc;
clear variables;
close all;
rand('state',32768);
randn('state',86723);
warning('off','comm:obsolete:randint')
N = 128*128*8; %Data lenghth
Pt = 0:2:40; %transmit power (dBm)
pt = (10^-3)*10.^(Pt/10); %transmit power (linear scale)
BW = 1*10^6; %BandWidth = 1MHZ
No = -174 + 10*log10(BW); %Noise power (dBm)
no = (10^-3) *10.^(No/10); %Noise power (linear scale)
d1 = 500; d2 = 200; d3 = 70; %Distances
a1 = 0.8; a2 = 0.15; a3 = 0.05; %power allocation coefficients
eta = 4; %path loss
% Generate Rayleigh fading channel for the three users
h1 = sqrt(d1^-eta)*(randn(N/2,1) + 1i*randn(N/2,1))/sqrt(2);
g1 = (abs(h1)).^2;
h2 = sqrt(d2^-eta)*(randn(N/2,1) + 1i*randn(N/2,1))/sqrt(2);
g2 = (abs(h2)).^2;
h3 = sqrt(d3^-eta)*(randn(N/2,1) + 1i*randn(N/2,1))/sqrt(2);
g3 = (abs(h3)).^2;
% Generate noise samples for the three users
n1 = sqrt(no)*(randn(N/2,1) + 1i*randn(N/2,1))/sqrt(2);
n2 = sqrt(no)*(randn(N/2,1) + 1i*randn(N/2,1))/sqrt(2);
n3 = sqrt(no)*(randn(N/2,1) + 1i*randn(N/2,1))/sqrt(2);
%Generate binary message data ((image)) for the three Users----------------
original1_image = imread('pp2.jpeg');
%%%%%%%%%[M1,N1]=size(original1_image);
nByte1 = numel(original1_image);
[original1_Data,row_im, col_im, third_im] = image2data(original1_image, 2);%image 2 Data conversion
x1=0.1;
u1=4;
%Iterative200Second, achieve full chaotic state
for i1=1:200
x1=u1*x1*(1-x1);
end
%Generate a single-dimensional chaotic encryption sequence
A1=zeros(1,nByte1); %generate1*Mn zero matrix
A1(1)=x1;
%Generate chaotic sequence
for i1=1:nByte1-1
A1(i1+1)=u1*A1(i1)*(1-A1(i1));
end
%Normalized sequence
B1=uint8(255*A1); %Convert to 255 type of data
%Transforming into two-dimensional chaotic encryption sequence
%%%%%%%%%%5Cipher1 =reshape(B1,M1,N1); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N column
Cipher1 = reshape(B1, size(original1_image)); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encry1 =bitxor(original1_Data',Cipher1); %Tone or operation encryption
Encrypted1 = reshape (Encry1,N,1);
Encrypted1_image = data2image(Encrypted1', row_im, col_im, third_im, 2); %Data 2 image conversion
%--------------------------------------------------------------------------
original2_image = imread('Woman.bmp');
%%%%%%%%%%%%5[M2,N2]=size(original2_image);
nByte2 = numel(original2_image);
[original2_Data,row_im, col_im, third_im] = image2data(original2_image, 2);%image 2 Data conversion
x2=0.1;
u2=4;
%Iterative200Second, achieve full chaotic state
for i2=1:200
x2=u2*x2*(1-x2);
end
%Generate a single-dimensional chaotic encryption sequence
A2=zeros(1,nByte2); %generate1*Mn zero matrix
A2(1)=x2;
%Generate chaotic sequence
for i2=1:nByte2-1
A2(i2+1)=u2*A2(i2)*(1-A2(i2));
end
%Normalized sequence
B2=uint8(255*A2); %Convert to 255 type of data
%Transforming into two-dimensional chaotic encryption sequence
%%%%%Cipher2=reshape(B2,M2,N2); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Cipher2 = reshape(B2, size(original2_image)); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encry2 =bitxor(original2_Data' ,Cipher2); %Tone or operation encryption
Encrypted2 = reshape (Encry2,N,1);
Encrypted2_image = data2image(Encrypted2', row_im, col_im, third_im, 2); %Data 2 image conversion
%--------------------------------------------------------------------------
original3_image = imread('Clock.bmp');
%%%%%5[M3,N3]=size(original3_image);
nByte3 = numel(original3_image);
[original3_Data,row_im, col_im, third_im] = image2data(original3_image, 2);%image 2 Data conversion
x3=0.1;
u3=4;
%Iterative200Second, achieve full chaotic state
for i3=1:200
x3=u3*x3*(1-x3);
end
%Generate a single-dimensional chaotic encryption sequence
A3=zeros(1,nByte3); %generate1*Mn zero matrix
A3(1)=x3;
%Generate chaotic sequence
for i3=1:nByte3-1
A3(i3+1)=u3*A3(i3)*(1-A3(i3));
end
%Normalized sequence
B3=uint8(255*A3); %Convert to 255 type of data
%%%%Cipher3=reshape(B3,M3,N3); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Cipher2 = reshape(B3, size(original3_image)); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encry3 =bitxor(original3_Data' ,Cipher3); %Tone or operation encryption
Encrypted3 = reshape (Encry3,N,1);
Encrypted3_image = data2image(Encrypted3', row_im, col_im, third_im, 2); %Data 2 image conversion
%create QPSKModulator and QPSKDemodulator objects
QPSKmod = comm.QPSKModulator('BitInput',true);
QPSKdemod = comm.QPSKDemodulator('BitOutput',true);
%perform QPSK modulation
mod1 = step(QPSKmod,Encrypted1);
mod2 = step(QPSKmod,Encrypted2);
mod3 = step(QPSKmod,Encrypted3);
%Do superposition coding
x = sqrt(a1)*mod1 + sqrt(a2)*mod2 + sqrt(a3)*mod3 ;
for u = 1:length(Pt)
%received signals
y1 = sqrt(pt(u))*x.*h1 + n1;
y2 = sqrt(pt(u))*x.*h2 + n2;
y3 = sqrt(pt(u))*x.*h3 + n3;
%perform equalization
eq1 = y1./h1; %At user 1
eq2 = y2./h2; %At user 2
eq3 = y3./h3; %At user 3
%Demode at user1 (direct decoding)
dem1 = step(QPSKdemod, eq1);
demodulated1_image = data2image(dem1', row_im, col_im, third_im, 2); %Data 2 image conversion
%Demode at user2
dem12 = step(QPSKdemod, eq2); %Direct demodulation to get user1’s data
dem12_remod = step(QPSKmod, dem12); %Remodulation of user1’s data
rem2 = eq2 - sqrt(a1*pt(u))*dem12_remod; %SIC to remove user1’s data
dem2 = step(QPSKdemod, rem2); %direct demodulation of remaining signal
demodulated2_image = data2image(dem2', row_im, col_im, third_im, 2); %Data 2 image conversion
%Demode at user3
dem13 = step(QPSKdemod, eq3); %direct demodulation to get user1’s data
dem13_remod = step(QPSKmod, dem13); %Remodulation of user1’s data
rem31 = eq3 - sqrt(a1*pt(u))*dem13_remod; %SIC to remove user1’s data
dem23 = step(QPSKdemod, rem31); %direct demodulation of remaining signal to get user2’s data
dem23_remod = step(QPSKmod, dem23); %Remodulation of user2’s data
rem3 = rem31 - sqrt(a2*pt(u))*dem23_remod; %SIC to remove user2’s data
dem3 = step(QPSKdemod, rem3); %demodulate of remaining signal to get user3’s data
demodulated3_image = data2image(dem3', row_im, col_im, third_im, 2); %Data 2 image conversion
%BER calculation
ber1(u) = biterr(dem1, Encrypted1)/N;
ber2(u) = biterr(dem2, Encrypted2)/N;
ber3(u) = biterr(dem3, Encrypted3)/N;
end
Decrypted1=bitxor(dem1,Cipher1); %Decryption(XOR operation)
Decrypted1_image = data2image(Decrypted1', row_im, col_im, third_im, 2); %Data 2 image conversion
Decrypted2=bitxor(dem2,Cipher2); %Decryption(XOR operation)
Decrypted2_image = data2image(Decrypted2', row_im, col_im, third_im, 2); %Data 2 image conversion
Decrypted3=bitxor(dem3,Cipher3); %Decryption(XOR operation)
Decrypted3_image = data2image(Decrypted3', row_im, col_im, third_im, 2); %Data 2 image conversion
%--------------------------------------------------------------------------
figure(1)
subplot(1,4,1);
imshow(original1_image,[]);
subplot(1,4,2);
imshow(Encrypted1_image,[]);
subplot(1,4,3);
imshow(demodulated1_image,[]);
subplot(1,4,4);
imshow(Decrypted1_image,[]);
figure(2)
subplot(1,4,1);
imshow(original2_image,[]);
subplot(1,4,2);
imshow(Encrypted2_image,[]);
subplot(1,4,3);
imshow(demodulated2_image,[]);
subplot(1,4,4);
imshow(Decrypted2_image,[]);
figure(3)
subplot(1,4,1);
imshow(original3_image,[]);
subplot(1,4,2);
imshow(Encrypted3_image,[]);
subplot(1,4,3);
imshow(demodulated3_image,[]);
subplot(1,4,4);
imshow(Decrypted3_image,[]);
figure (4)
semilogy(Pt, ber1, 'b-o', 'linewidth', 2);
hold on; grid on ;
semilogy(Pt, ber2, 'r-s', 'linewidth', 2);
semilogy(Pt, ber3, 'k->', 'linewidth', 2);
xlabel('Transmit power (dBm)');
ylabel('BER');
legend('User 1 (Weakest user)', 'User 2', 'User 3 (Strongest user)');
Now you are using
Encry1 = bitxor(original1_Data',Cipher1);
The tranpspose operator ' works with matrices only. In the first example your data was an RGB image, which is imported as 3D array. Not the original data are a 2D matrix. Why do you transpose it?
I cannot run your code due to themissing images and the functions "data2image" and "image2data". But you can use the debugger to examine, what's going on: Set a break point in the first line and step through the code. The problem concerns the dimension of the image data and the cipher. Clarify what happens with RGB and grayscale images.
data2image:
%--------------------------------------------------------------------------
%this function is to transfer data type back to image for display
%
%Chen Zhifeng
%2007-05-19
%zhifeng@ecel.ufl.edu
%I left this function as is since I'm clueless and a neophyte in image
%processing. JC 7/16/08
%--------------------------------------------------------------------------
function im = data2image(Data, row_im, col_im, third_im, M);%changed data to Data
% M =4;
data = Data';%changed data' to Data'
l = length(data);
col =ceil( 8/log2(M) ); %in case M = 8, 32, 64, 128, we need ceil
row= l/col;
Tm = zeros(row,col);
for i = 1:col,
Tm(:,i) = num2str(data((i-1)*row+1 : i*row));
end
%temp = char(Tm);
%Tmn = num2str(temp);
V_im = base2dec(Tm,M);
im = zeros(row_im, col_im, third_im);
for i=1:third_im,
for j=1:col_im,
im(:,j,i) = V_im((i-1)*row_im*col_im+(j-1)*row_im +1 : (i-1)*row_im*col_im+j*row_im);
end
end
image2data:
%--------------------------------------------------------------------------
%this function is to transfer image to data suitable for transmission
%
%Chen Zhifeng
%2007-05-19
%zhifeng@ecel.ufl.edu
%%I left this function as is since I'm clueless and a neophyte in image
%processing. JC 7/16/08
%--------------------------------------------------------------------------
function [data, row_im, col_im, third_im] = image2data(im, M)
% im = imread('photo.bmp');
% M =4;
[row_im, col_im, third_im] =size(im);
V_im = zeros(1, row_im*col_im*third_im);
for i=1:third_im,
for j=1:col_im,
V_im((i-1)*row_im*col_im+(j-1)*row_im +1 : (i-1)*row_im*col_im+j*row_im) = im(:,j,i);
end
end
%I use dec2base here, then if M>8, for example M=16, the result string may
%include characters, this is not appropriate to use str2num below. However,
%due to the time limit, I will not add function here to deal with this.
%Actually, this may be done by dec2bin function.
Tm = dec2base(V_im,M);
[row, col] = size(Tm);
% data = [];
% for i = 1:col,
% data = [data; Tm(:,i)];
% end
%
%data=[];
for i =1:col,
data(row*(i-1)+1:row*i)=str2num(Tm(:,i)); %very important to add str2num here
end
%Tm = str2num(Tm_char);
%data = str2num(data);
Images are normal grey scale images , i used rgb by mistake
my problem is : how to get the image data & the cipher of the same dimensions to accomplish the bitxor function ?

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2016a

Tags

Community Treasure Hunt

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

Start Hunting!