0% found this document useful (0 votes)
61 views2 pages

All All 'Lena512.bmp': % I Magic

This document loads an image, extracts its bits, encrypts the bits using XOR operations with a random key, decrypts the bits, and reconstructs the encrypted image. It: 1) Loads an image, extracts its bits into 8 variables, and generates a random key. 2) Encrypts the bits by XORing bits from the image and key in blocks of 4 and 2 bits. 3) Decrypts the bits by XORing the encrypted bits with the key to reconstruct the original image bits.

Uploaded by

Neetesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

All All 'Lena512.bmp': % I Magic

This document loads an image, extracts its bits, encrypts the bits using XOR operations with a random key, decrypts the bits, and reconstructs the encrypted image. It: 1) Loads an image, extracts its bits into 8 variables, and generates a random key. 2) Encrypts the bits by XORing bits from the image and key in blocks of 4 and 2 bits. 3) Decrypts the bits by XORing the encrypted bits with the key to reconstruct the original image bits.

Uploaded by

Neetesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

close all;

clear all;
clc;
I=double(imread('lena512.bmp'));
figure; imshow(I,[]);
% I=magic(5);
[x,y]=size(I);
[p8,p7,p6, p5, p4,p3,p2, p1]=bit_extract(I);
m=round(rand(1,768900));
%%%%%%%%m=[1 0 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1];
f=length(m);
if(rem(f,4)==1)
m=[m,0,0,0];
elseif(rem(f,4)==2)
m=[m,0,0];
elseif(rem(m,4)==3)
m=[m,0];
else
end
f=length(m);
U=[p1,p2,p3,p4];
A=U(:);
l=1;
i=1;
while(l<=f-3 && i<=5*floor(2*x*y/5)-4)
k1=xor(A(i),A(i+1));
k2=xor(A(i+2),A(i+3));
k3=xor(A(i),A(i+2));
k4=xor(1,A(i+4));
if(m(l)==k1 && m(l+1)==k2 && m(l+2)==k3 && m(l+3)~=k4)
A(i+4)=not(A(i+4));
elseif(m(l)==k1 && m(l+1)==k2 && m(l+2)~=k3&& m(l+3)==k4)
A(i)=not(A(i));A(i+1)=not(A(i+1));
elseif(m(l)==k1 && m(l+1)==k2 && m(l+2)~=k3&& m(l+3)~=k4)
A(i)=not(A(i));A(i+1)=not(A(i+1)); A(i+4)=not(A(i+4));
elseif(m(l)==k1 && m(l+1)~=k2 && m(l+2)==k3&& m(l+3)==k4)
A(i+3)=not(A(i+3));
elseif(m(l)==k1 && m(l+1)~=k2 && m(l+2)==k3&& m(l+3)~=k4)
A(i+3)=not(A(i+3));A(i+4)=not(A(i+4));
elseif(m(l)==k1 && m(l+1)~=k2 && m(l+2)~=k3 && m(l+3)==k4)
A(i+2)=not(A(i+2));
elseif(m(l)==k1 && m(l+1)~=k2 && m(l+2)~=k3&& m(l+3)~=k4)
A(i+2)=not(A(i+2)); A(i+4)=not(A(i+4));
elseif(m(l)~=k1 && m(l+1)==k2 && m(l+2)==k3&& m(l+3)==k4)
A(i+1)=not(A(i+1));
elseif(m(l)~=k1 && m(l+1)==k2 && m(l+2)==k3&& m(l+3)~=k4)
A(i+1)=not(A(i+1)); A(i+4)=not(A(i+4));
elseif(m(l)~=k1 && m(l+1)==k2 && m(l+2)~=k3&& m(l+3)==k4)
A(i)=not(A(i));
elseif(m(l)~=k1 && m(l+1)==k2 && m(l+2)~=k3&& m(l+3)~=k4)
A(i)=not(A(i));A(i+4)=not(A(i+4));
elseif(m(l)~=k1 && m(l+1)~=k2 && m(l+2)==k3&& m(l+3)==k4)
A(i+1)=not(A(i+1));A(i+3)=not(A(i+3));
elseif(m(l)~=k1 && m(l+1)~=k2 && m(l+2)==k3&& m(l+3)~=k4)
A(i+1)=not(A(i+1)); A(i+4)=not(A(i+4));A(i+3)=not(A(i+3));
elseif(m(l)~=k1 && m(l+1)~=k2 && m(l+2)~=k3 && m(l+3)==k4)
A(i+1)=not(A(i+1)); A(i+2)=not(A(i+2));
elseif(m(l)~=k1 && m(l+1)~=k2 && m(l+2)~=k3 && m(l+3)~=k4)
A(i)=not(A(i)); A(i+3)=not(A(i+3)); A(i+4)=not(A(i+4));
else
A(i)=A(i);A(i+1)=A(i+1);A(i+2)=A(i+2);A(i+3)=A(i+3);A(i+4)=A(i+4);

end
l=l+4;i=i+5;
end
i=2*x*y+1;
while(l<=f-1 && i<=3*floor(4*4*x*y/3)-2)
k1=xor(A(i),A(i+1));
k2=xor(A(i+1),A(i+2));
if(m(l)==k1 && m(l+1)~=k2)
A(i+2)=not(A(i+2));
l=l+2;i=i+3;
elseif(m(l)~=k1 && m(l+1)==k2)
A(i)=not(A(i));
l=l+2;i=i+3;
elseif(m(l)~=k1 && m(l+1)~=k2)
A(i+1)=not(A(i+1));
l=l+2;i=i+3;
else
l=l+2;i=i+3;
end
end
q1=A(1:x*y);
q2=A(x*y+1:2*x*y);
q3=A(2* x*y+1:3*x*y);
q4=A(3*x*y+1:4*x*y);
q1=reshape(q1,x,y);
q2=reshape(q2,x,y);
q3=reshape(q3,x,y);
q4=reshape(q4,x,y);
S=p8*2^7+p7*2^6+p6*2^5+p5*2^4+q4*2^3+q3*2^2+q2*2^1+q1*1;
figure
imshow(S,[]);
MSE=sum((I(:)-S(:)).^2)/numel((I));
PSNR=10*log(255*255/MSE);
[q8, q7, q6, q5, q4, q3, q2, q1]=(bit_extract(S));
B=[q1,q2,q3,q4];
B=B(:);
R=[];l=1;i=1;
while(l<=f-3 && i<=5*floor(2*x*y/5)-4)
R(l)=xor(A(i),A(i+1));
R(l+1)=xor(A(i+2),A(i+3));
R(l+2)=xor(A(i),A(i+2));
R(l+3)=xor(1,A(i+4));
l=l+4;i=i+5;
end
i=2*x*y+1;
while(l<=f-1 && i<=3*floor(4*4*x*y/3)-4)
R(l)=xor(A(i),A(i+1));
R(l+1)=xor(A(i+1),A(i+2));
l=l+2;i=i+3;
end

You might also like