clear all
clc
Picture =imread('lenna.bmp');
P=double((Picture));
subplot(2,3,1);imshow(Picture);title('Tx - Original Image');
R=P(:,:,1);
G=P(:,:,2);
B=P(:,:,3);
%%%%%%%%%%%%%%%% BB84 %%%%%%%%%%%%%%%%%%%%%%%%%
Q1=round(rand(128,2));
Q2=round(rand(128,1));
m=1;
for i=1:128
if Q1(i,2)==Q2(i)
QKEY(m)=Q1(i,1);
m=m+1;
end
end
n=1;
for i=1:512*512*8
if n>=length(QKEY)
n=0;
end
n=n+1;
secret_key(i)=QKEY(n);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
key=de2bi(round(rand(512,512,8)));
key=key';
shared_key=bitxor(secret_key,key);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
R1 = de2bi(R);
for i=1:length(R1)
k = key((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(R1(i,:),k);
R1(i,:) = end_code;
end
G1 = de2bi(G);
for i=1:length(G1)
k = key((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(G1(i,:),k);
G1(i,:) = end_code;
end
B1 = de2bi(B);
for i=1:length(B1)
k = key((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(B1(i,:),k);
B1(i,:) = end_code;
end
r1 = bi2de(R1);
g1 = bi2de(G1);
b1 = bi2de(B1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TP=[r1,g1,b1];
TP1=[R1,G1,B1];
TP2=[R1,G1,B1];
TP3=[R1,G1,B1];
tp=reshape(TP,512,512,3);
size(tp)
tp=uint8(tp);
subplot(2,3,2);imshow(tp);title('Tx - ideal encrypted image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% TP1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z=zeros(1,24);
N=randperm(262144,26224);
for i=1:26224
A=N(1,i);
TP1(A,:)=Z;
end
r2=bi2de(TP1(:,1:8));
g2=bi2de(TP1(:,9:16));
b2=bi2de(TP1(:,17:24));
TP1=[r2,g2,b2];
tp1=reshape(TP1,512,512,3);
size(tp1)
tp1=uint8(tp1);
subplot(2,3,3);imshow(tp1);title('Tx - encrypted image with noise');
%%%%%%%%%%%%%%%%%%%%%%%%%%%% TP2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z=zeros(1,24);
N=randperm(262144,26224);
for i=1:26224
A=N(1,i);
TP2(A,:)=Z;
end
r3=bi2de(TP2(:,1:8));
g3=bi2de(TP2(:,9:16));
b3=bi2de(TP2(:,17:24));
TP2=[r3,g3,b3];
tp2=reshape(TP2,512,512,3);
size(tp2)
tp2=uint8(tp2);
%figure;imshow(tp2);title('Tx - encrypted image 2');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TP 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z=zeros(1,24);
N=randperm(262144,26224);
for i=1:26224
A=N(1,i);
TP3(A,:)=Z;
end
r4=bi2de(TP3(:,1:8));
g4=bi2de(TP3(:,9:16));
b4=bi2de(TP3(:,17:24));
TP3=[r4,g4,b4];
tp3=reshape(TP3,512,512,3);
size(tp3)
tp3=uint8(tp3);
%figure;imshow(tp3);title('Tx - encrypted image 3');
%%%%%%%%%%%%%%%%%%%%%%%%%%%ideal decode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
key_D=bitxor(secret_key,shared_key);
tp_D=double((tp));
R1_D=tp_D(:,:,1);
G1_D=tp_D(:,:,2);
B1_D=tp_D(:,:,3);
R1_D = de2bi(R1_D);
for i=1:length(R1_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(R1_D(i,:),k);
R1_D(i,:) = end_code;
end
R1_D = bi2de(R1_D);
G1_D = de2bi(G1_D);
for i=1:length(G1_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(G1_D(i,:),k);
G1_D(i,:) = end_code;
end
G1_D = bi2de(G1_D);
B1_D = de2bi(B1_D);
for i=1:length(B1_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(B1_D(i,:),k);
B1_D(i,:) = end_code;
end
B1_D = bi2de(B1_D);
R1_D = reshape(R1_D, 512,512);
G1_D = reshape(G1_D, 512,512);
B1_D = reshape(B1_D, 512,512);
pp_D = [R1_D G1_D B1_D];
PP_D = reshape(pp_D, 512,512,3);
%
size(tp_D)
size(PP_D)
PP_D=uint8(PP_D);
subplot(2,3,4);
imshow(PP_D);title('ideal dencrypted image');
% %%%%%%%%%%%%%%%%%%%%%%%%% decode 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tp2_D=double((tp2));
R3_D=tp2_D(:,:,1);
G3_D=tp2_D(:,:,2);
B3_D=tp2_D(:,:,3);
R3_D = de2bi(R3_D);
for i=1:length(R3_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(R3_D(i,:),k);
R3_D(i,:) = end_code;
end
R3_D = bi2de(R3_D);
G3_D = de2bi(G3_D);
for i=1:length(G3_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(G3_D(i,:),k);
G3_D(i,:) = end_code;
end
G3_D = bi2de(G3_D);
B3_D = de2bi(B3_D);
for i=1:length(B3_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(B3_D(i,:),k);
B3_D(i,:) = end_code;
end
B3_D = bi2de(B3_D);
R3_D = reshape(R3_D, 512,512);
G3_D = reshape(G3_D, 512,512);
B3_D = reshape(B3_D, 512,512);
pp2_D = [R3_D G3_D B3_D];
PP2_D = reshape(pp2_D, 512,512,3);
%
size(PP2_D)
PP2_D=uint8(PP2_D);
subplot(2,3,5);
imshow(PP2_D);title('noised image ');
%%%%%%%%%%%%%%%% Error Correction%%%%%%%%%%%%%%%%%%%%
tp1=de2bi(double(tp1));
tp2=de2bi(double(tp2));
tp3=de2bi(double(tp3));
for i=1:786432
for h=1:8
if tp1(i,h)==tp2(i,h)
else if tp1(i,h)==tp3(i,h)
else tp1(i,h)=tp2(i,h);
end
end
end
end
tp1_D=bi2de(tp1);
R2_D=tp1_D(1:512*512);
G2_D=tp1_D(512*512+1:512*512*2);
B2_D=tp1_D(512*512*2+1:512*512*3);
R2_D = de2bi(R2_D);
for i=1:length(R2_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(R2_D(i,:),k);
R2_D(i,:) = end_code;
end
R2_D = bi2de(R2_D);
G2_D = de2bi(G2_D);
for i=1:length(G2_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(G2_D(i,:),k);
G2_D(i,:) = end_code;
end
G2_D = bi2de(G2_D);
B2_D = de2bi(B2_D);
for i=1:length(B2_D)
k = key_D((i-1)*8+1:(i-1)*8+8);
end_code = bitxor(B2_D(i,:),k);
B2_D(i,:) = end_code;
end
B2_D = bi2de(B2_D);
R2_D = reshape(R2_D, 512,512);
G2_D = reshape(G2_D, 512,512);
B2_D = reshape(B2_D, 512,512);
pp1_D = [R2_D G2_D B2_D];
PP1_D = reshape(pp1_D, 512,512,3);
%
size(tp1_D)
size(PP1_D)
PP1_D=uint8(PP1_D);
subplot(2,3,6);
imshow(PP1_D);title('corrected image');