# 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.