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

While End While End: % RX (RX 0) - 64

This document contains code for encrypting and decrypting messages using RSA encryption. It defines variables for the encryption keys, asks the user to select a message type, encrypts the input message using the encryption key, displays the encrypted message, decrypts it using the decryption key, and displays the decrypted message.

Uploaded by

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

While End While End: % RX (RX 0) - 64

This document contains code for encrypting and decrypting messages using RSA encryption. It defines variables for the encryption keys, asks the user to select a message type, encrypts the input message using the encryption key, displays the encrypted message, decrypts it using the decryption key, and displays the decrypted message.

Uploaded by

Dhanmathy Vikash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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