function dimg =decryptionprocess2_CBC(img,h,RK,Inv_SubBytes,Inv_Pbox,r,IV)
%%%%%%%%%%%%%% convert image to a vector seq
%seq=double(reshape(img,1,[]));
seq_bin=double(byte2bit(uint8(img))) ;
decr_seq_binary=zeros(1,length(seq_bin));
%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%%
nb= floor(length(seq_bin)/(8*h));
RKB=byte2bit(RK);
RKB=RKB(1:r*h*8);
%RK_bin=double(reshape(byte2bit(RK),r,[]));
RK_bin=double(reshape(RKB,r,[]));
%RK_bin=double(reshape(byte2bit(RK),r,[]));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% Convert S-box to binary representation to avoid repeat the
%%%%%%%%%%% byte2bit conversion
Bin_Sbox  =double(Convert_Sbox_Bin(Inv_SubBytes));

iv=[];
for i=1:h
    iv=[iv byte2bit(IV(i))];
end
IV=double(iv);


%%%%%%%%%%%%%%%%%%%% Applied the confusion process (r rounds)
for it=1:nb
     temp=seq_bin((it-1)*8*h+1:it*h*8);
     temp_IV=temp;
    for itr=r:-1:1       
        temp= temp(Inv_Pbox);
        temp=reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]);
        temp=bitxor(temp,RK_bin(itr,1:h*8));   
    end
    temp=bitxor(temp,IV);
    decr_seq_binary((it-1)*8*h+1:it*h*8)=temp;
    IV= temp_IV;
end

% Convert to Byte representation

%valbin=reshape(valbin,Tb/8,8);
dimg=uint8(bit2byte(decr_seq_binary));