You are on page 1of 2

clc;

clear all;
close all;
p=3;
q=11;
n=p*q;
z=(p-1)*(q-1);
d=2;
e=2;

while(gcd(d,z)>1)
d=d+1;
end
while(mod(d*e,z)>1)
e=e+1;
end
disp(sprintf('\n the encrypting key is %d',e));
disp(sprintf('\n the decrypting key is %d',d));
disp(sprintf('\n1. message using capital letters and symbols :;<=>?@ '));
disp(sprintf('\n2. message using small letters and symbols {}|~ '));
disp(sprintf('\n3. message using numbers and remaining symbols'));
disp(sprintf('\n4. message of any type'));
ch=input('\n Enter your choice ');
m=input('\n enter the message to be encrypted ','s');
switch ch

case 1
t=double(m)-57;
t(find(t<1))=0;
for i=1:length(m)
g(i)=mod(t(i)^e,n);
end
disp(sprintf('\n encrypted message is : \n'));
disp(g);
for i=1:length(m)
rx(i)=mod(g(i)^d,n);
end
rx(rx<=0)=-2;
r=char((rx)+57);
case 2
t=double(m)-96;
t(t<1)=0;
for(i=1:length(m))
g(i)=mod(t(i)^e,n);
end
disp(sprintf('\n encrypted message is \n'));
disp(g);
for i=1:length(m)
rx(i)=mod(g(i)^d,n);
end
% rx(rx<=0)=-64;
r=char(rx+96);
case 3
t=double(m)-32;
for i=1:length(m)
g(i)=mod(t(i)^e,n);
end
disp(sprintf('\n encrypted message is \n'));
disp(g);
for i=1:length(m)
rx(i)=mod(g(i)^d,n);
end
r=char(rx+32);
case 4
t=double(m);
len=length(m);
cd1=find(t>=66 & t<99);
cd2=find(t>=99);
cd1=[cd1,zeros(1,len-length(cd1))];
cd2=[cd2,zeros(1,len-length(cd2))];
for i=1:len
g(i)=mod(t(i)^e,n);
end
disp(sprintf('\n encrypted message is \n'));
disp(g);
for i=1:length(m)
rx(i)=mod(g(i)^d,n);
end
j=1;k=1;
for i=1:len
if i==cd1(j)
r(i)=char(rx(i)+66);
j=j+1;
else if i==cd2(k)
r(i)=char(rx(i)+99);
k=k+1;
else
r(i)=char(rx(i)+33);
end
end
end

end
disp(sprintf('\n decrypted message is \n'));
disp(r);

You might also like