Vous êtes sur la page 1sur 10

ECE 106

RSA Encryption,
Decryption and Key
Generation (MATLAB)

Gerfel Philip Gonzales


March, 2015

1 KEY GENERATION MATLAB CODE


%% Initialization
clear all;
clc;
rng('shuffle');
state = randi([100 999],1,2);
%%Generate Random Public Exponent e
e = randseed(state(1,1),1,1,1,20);
%%Generate Random Prime Numbers
P1 = randseed(state(1,1),1,1,50,99);
P2 = randseed(state(1,2),1,1,50,99);
%%Acquire the Modulo
N = P1*P2; %this is the Modulo
PHIN = totient(N);
%%Solve for Private Exponent d using Extended Euclidean Algorithm
% Forward Euclidean
[d, q, a, b, f] = extendedEuclidean_forward(PHIN,e);
% Euclidean (method of back-substitution)
[x, y] = extendedEuclidean_backSubstitution (d, q, a, b, f);
d = PHIN + x; %this is the private key
%%Print Results
publickey = '\nPublic Key is e = %d \nPublic Modulo is N = %d\n\n';
privatekey = 'Private Key is d = %d\n\n';
fprintf(publickey,e,N);

fprintf(privatekey,d);

This is the matlab file used for generating both public keys and private key.
First part is the Initialization. What it does is it clears the stored variables and values
in the workspace and clears the window off characters. The function rng(shuffle) resets
the randomizing function of the MATLAB in order to generate different numbers in every
time randi is called.
Then, public exponent e, is generated using randseed function. This function
randomly generates prime numbers ranges from 1 to 20. The two prime numbers P1 and
P2 are generated. Same as the public exponent, it randomly generates prime numbers
ranges from 50 to 99.
Multiplying P1 and P2 gives the public modulo N. Euler Phi number is acquired using
totient function. Now, the inverse modulo is acquired using the Extended Euclidean
Algorithm. This algorithm is used for finding inverse modulo with large exponents.
Private Key can now be acquired. Just sum PHIN and x. Then print the results in
matlab window space.

Gerfel Philip Gonzales

2 ENCRYPTION MATLAB CODE


%% Encryption
function c = encrypt()
str = input('Code to be encrypted? str = ', 's')
e = input('Enter the Public Key e = ')
N = input('Enter the Public Modulo N = ')
num = double(str); %convert string to decimal
[f,g]=size(num);
k = e;
n = N;
%%Modular Exponentiation
for i = 1:g
x = num(1,i);
% y = x^e mod n
exponent_track = k;
y = 1;
while (exponent_track > 0)
y = y*x;
while ((y - n) > n)
y = y - n;
end
exponent_track = exponent_track - 1;
end
if (y > n)
y = y - n;
end
y;
c(1,i) = modularExponentiation( x, k, n );
end
%Print Results
encryptedmsg = '\nEncrypted Message: \n';
fprintf(encryptedmsg);
return
end

This is the matlab file used for Encryption.


The acquiring of the essential requirements in encryption is done, these are, the
public exponent e, public modulo N and of course, the code that is about to be encrypted.
Convert the string in ASCII or Unicode into Decimal code. This done using
double(str) function.
With all the requirement present, encryption can now be done. Encryption is just
using this formula

x mod n

, where

is the code to encrypted,

is the exponent

Gerfel Philip Gonzales

and

is the public modulo. This formula can be done using a numerical method called

Modular Exponentiation.
Then it returns c, the now encrypted code or message. Print the results in matlab
window space.

3 DECRYPTION MATLAB CODE


%% Decryption
function r = decrypt()
num = input('Code to be decrypted? num = ')
e = input('Enter the Private Key d = ')
N = input('Enter the Public Modulo N = ')
[f,g]=size(num);
k = e;
n = N;
%%Modular Exponentiation
for i = 1:g
x = num(1,i);
% y = x^e mod n
exponent_track = k;
y = 1;
while (exponent_track > 0)
y = y*x;
while ((y - n) > n)
y = y - n;
end
exponent_track = exponent_track - 1;
end
if (y > n)
y = y - n;
end
y;
c(1,i) = modularExponentiation( x, k, n );
end
%Convert Decimal to ASCII
r = native2unicode(c, 'US-ASCII');
%Print Result
encryptedmsg = '\nDecrypted Message: \n';
fprintf(encryptedmsg);
return
end

Decryption is done just like encryption but this time, it uses the private key. This is
the matlab file used for Decryption.
The acquiring of the essential requirements in decryption is done, these are, the
private exponent d, public modulo N and of course, the code that is about to be decrypted.

Gerfel Philip Gonzales

With all the requirement present, decryption can now be done. decryption is just
e

c=x mod n , where

using this formula


exponent d and

is the code to decrypted,

is the private

is the public modulo. This formula can be done using a numerical

method called Modular Exponentiation.


Then it returns c, in decimal form. Convert the decimal code into a string in ASCII or
Unicode. This now the decrypted code or message. Print the results in matlab window
space.

4 SIDE FUNCTIONS
%TOTIENT
function [t] = totient(n)
[r c]=size(n);
n=reshape(n,1,r*c);
t=zeros(1,r*c);
f=zeros(1,10);
for k=1:r*c;
nk=n(k);
f=unique(factor(nk));
t(k)=nk*prod(1-1./f);
end
t=reshape(t,r,c);
p=find(n==1);
t(p)=1;
t=round(t);
return

%% Extended Euclidean Back-Substitution


function [x,y] = extendedEuclidean_backSubstitution
(d,q,a,b,f)
x = 0;
y = 1;
% print steps
numberOfSteps = numel(f) - 1;
step = 1;
for i = (numberOfSteps):-1:1
if (mod(step, 2) ~= 0)
x = x + y * f(i);
else
y = y + x * f(i);
end
step = step + 1;
end
x = x * (-1);
end

Gerfel Philip Gonzales

%% Extended Euclidean Forward


function [d,q,a,b,f] =
extendedEuclidean_forward(p,q)
f = [];
while (q(end) ~= 0)
f = [f floor(p(end)/q(end))];
p = [p q(end)];
q = [q mod(p(end-1), q(end))];
end
% updates
q = q(1:(end));
d = p(1:(end-1));
a = 1;
b = 0;
end

%% Modular Exponentiation operations for big interger base^exponent


function result = modularExponentiation( base, exponent, n )
% toy de-/ en-ciphering
exponent_track = exponent;
result = 1;
% result = mod (exponent^d, n)
while (exponent_track > 0)
result = result*base;
while ((result - n) > n)
result = result - n;
end
exponent_track = exponent_track - 1;
end
if (result > n)
result = result - n;
end
end

5 KEY GENERATION SIMULATION


1. Generate all the keys by calling generatekey() in matlab cmd window.

2. The Results:

Gerfel Philip Gonzales

This is the workspace where variables and values are stored.

This is the printed results in the Command window. Public key is 5, public modulo is
6499 and private key is 5069.
Public Modulo
Public Key
Private Key

6499
5
5069

Gerfel Philip Gonzales

6 ENCRYPTION SIMULATION
1. To do encryption, call encrypt()

2. Provide the essential requirement for encryption.


Message to be Encrypted
Public Key
Public Modulo

Hello World
5
6499

3. Results:

This is the encrypted message in decimal form and in matrix.

ans=[2857 2188 3620 3620347 952626 347 456 36201698]

Gerfel Philip Gonzales

7 DECRYPTION SIMULATION
1. Recover the encrypted message. Call decrypt()

2. Provide the essential requirement for decryption.


Message to be decrypted
Private Key
Public Modulo

[2857 2188 3620 3620 347 95 2626 347 456 3620 1698]
5069
6499

3. Result:

Clearly, message was decrypted and recovered correctly.

Gerfel Philip Gonzales

4. Now, decrypting with different private key to ensure correct key generation.
Message to be decrypted
Private Key
Public Modulo
Result

Message to be decrypted
Private Key
Public Modulo
Result

Message to be decrypted
Private Key
Public Modulo
Result

[2857 2188 3620 3620 347 95 2626 347 456 3620 1698]
5
6499

[2857 2188 3620 3620 347 95 2626 347 456 3620 1698]
589
6499

[2857 2188 3620 3620 347 95 2626 347 456 3620 1698]
5070
6499

Therefore, key generation was successful and effective.

Gerfel Philip Gonzales

Vous aimerez peut-être aussi