# Implementation of RSA Cryptography Algorithm Using MATLAB Code

Little background Information about RSA :
RSA is a cryptosystem, which is known as one of the first practicable public-key cryptosystems and is yet widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described the algorithm in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it wasn't declassified until 1997. (source: Wikipedia) For more background information you need, you can refer several research paper accessible through internet.

Let this system we implement in modular way. That is, we will be creating functions instead of a very long MATLAB file.

MATLAB codes for RSA Crytography Algorithm.

Sample Run Of the Code Below:

## d2b.m%function for conversion of decimal number to binary number

function a = d2b(d)

i=1;

a=zeros(1,65535);

while d >= 2

r=rem(d,2);

if r==1

a(i)=1;

else

a(i)=0;

end

i=i+1;

d=floor(d/2);

end

if d == 2

a(i) = 0;

else

a(i) = 1;

end
x=[a(16) a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5)]

## init.m   %For Initialization

function [Pk,Phi,d,e] = init(p,q)

clc;

disp('Initializing:');

Pk=p*q;

Phi=(p-1)*(q-1);

%For Calculating the value of e

x=2;e=1;

while x > 1

e=e+1;

x=gcd(Phi,e);

end

%For Calculating the value of d

i=1;

r=1;

while r > 0

k=(Phi*i)+1;

r=rem(k,e);

i=i+1;

end

d=k/e;

clc;

disp(['The value of (N) is: ' num2str(Pk)]);

disp(['The public key (e) is: ' num2str(e)]);

disp(['The value of (Phi) is: ' num2str(Phi)]);

disp(['The private key (d)is: ' num2str(d)]);

## crypto.m     %For Cryptography Key Generation

function mc = crypto(M,N,e)

e=d2b(e);

k = 65535;

c  = M;

cf = 1;

cf=mod(c*cf,N);

for i=k-1:-1:1

c = mod(c*c,N);

j=k-i+1;

if e(j)==1

cf=mod(c*cf,N);

end

end

mc=cf;

## medianet_width='728'; medianet_height= '20'; medianet_crid='690715163'; rsa.m%for RSA Algorithm

clc;

disp('Implementation of RSA Algorithm');

clear all;
close all;

p = input('\nEnter value of p: ');

q = input('\nEnter value of q: ');

[Pk,Phi,d,e] = init(p,q);

M = input('\nEnter message: ','s');

x=length(M);

c=0;

for j= 1:x

for i=0:122

if strcmp(M(j),char(i))

c(j)=i;

end

end

end

disp('ASCII Code of the entered Message:');

disp(c);

% For Encryption

for j= 1:x

cipher(j)= crypto(c(j),Pk,e);

end

disp('Cipher Text of the entered Message:');

disp(cipher);

% For Decryption

for j= 1:x

message(j)= crypto(cipher(j),Pk,d);

end

disp('Decrypted ASCII of Message:');

disp(message);

disp(['Decrypted Message is: ' message]);

NOTE: Place all the function saved in the current directory or accordingly change the current working directory. After saving all the function run the rsa.m file by just typing "rsa" in the command window. It will ask you the value of 'p' & 'q' & will generate the value of Public & private key.

1. can you please tell me how to run RSA code?
when i run rsa.m then it says
Implementation of RSA Algorithm

Enter the value of p: 3

Enter the value of q: 11
Undefined function 'intialize' for input arguments of type 'double'.
Error in RSA (line 6)
[Pk,Phi,d,e] = intialize(p,q);

1. Hi, The last line "disp(['Decrypted Message is: ' message]);" is showing empty. So, i rectified the above RSA code to take random prime numbers (range 10:100) which can displays the Decrypted message also. (take prime numbers which are greater than 10)

%%%RSA Implementation in matlab
clc;
disp('Implementation of RSA Algorithm');
clear all; close all;
range=10:100;
m=isprime(range);
prime_mat=range(find(m));
p=randsample(prime_mat,1);
q=randsample(prime_mat,1);
[Pk,Phi,d,e] = intialize(p,q);
M = input('\nEnter the message: ','s');
x=length(M);
c=0;
for j= 1:x
for i=0:122
if strcmp(M(j),char(i))
c(j)=i;
end
end
end
disp('ASCII Code of the entered Message:');
disp(c);
% % %Encryption
for j= 1:x
cipher(j)= crypt(c(j),Pk,e);
last_number=cipher(j);
sms=c(j);
end
disp('Cipher Text of the entered Message:');
disp(cipher);
disp('p=');
disp (p);
disp('q=');
disp (q);
% % %Decryption
for j= 1:x
message(j)= crypt(cipher(j),Pk,d);
end
disp('Decrypted ASCII of Message:');
disp(message);

disp(['Decrypted Message is: ' message]);

2. Hi, The last line "disp(['Decrypted Message is: ' message]);" is showing empty. So, i rectified the above RSA code to take random prime numbers (range 10:100) which can displays the Decrypted message also. (take prime numbers which are greater than 10)

%%%RSA Implementation in matlab
clc;
disp('Implementation of RSA Algorithm');
clear all; close all;
range=10:100;
m=isprime(range);
prime_mat=range(find(m));
p=randsample(prime_mat,1);
q=randsample(prime_mat,1);
[Pk,Phi,d,e] = intialize(p,q);
M = input('\nEnter the message: ','s');
x=length(M);
c=0;
for j= 1:x
for i=0:122
if strcmp(M(j),char(i))
c(j)=i;
end
end
end
disp('ASCII Code of the entered Message:');
disp(c);
% % %Encryption
for j= 1:x
cipher(j)= crypt(c(j),Pk,e);
last_number=cipher(j);
sms=c(j);
end
disp('Cipher Text of the entered Message:');
disp(cipher);
disp('p=');
disp (p);
disp('q=');
disp (q);
% % %Decryption
for j= 1:x
message(j)= crypt(cipher(j),Pk,d);
end
disp('Decrypted ASCII of Message:');
disp(message);

disp(['Decrypted Message is: ' message]);

2. The value of (N) is: 33
The public key (e) is: 3
The value of (Phi) is: 20
The private key (d)is: 7

Enter the message: hello
ASCII Code of the entered Message:
104 101 108 108 111
Cipher Text of the entered Message:
26 8 3 3 12
Decrypted ASCII of Message:
5 2 9 9 12
Decrypted Message is:

1. This comment has been removed by the author.

3. when I run, no error but no cipher text result. Only ASCII Code of message come out. Can u tell me why?

4. When I run, no error but no cipher text. Only ASCII code of message come out. Can u please tell me why?