RSA Encryption
RSA Encryption
RSA Encryption,
Decryption and Key
Generation (MATLAB)
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.
x mod n
, where
is the exponent
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.
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.
With all the requirement present, decryption can now be done. decryption is just
e
is the private
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
2. The Results:
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
6 ENCRYPTION SIMULATION
1. To do encryption, call encrypt()
Hello World
5
6499
3. Results:
7 DECRYPTION SIMULATION
1. Recover the encrypted message. Call decrypt()
[2857 2188 3620 3620 347 95 2626 347 456 3620 1698]
5069
6499
3. Result:
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