File Exchange

image thumbnail

RSA algorithm

version (1.47 KB) by suriyanath
RSA algorithm for encrypting and decrypting a message


Updated 17 Sep 2016

View Version History

View License

This code asks for Two prime numbers and then computes Public and Private key. Then the message is encrypted using Public key and decrypted using Private key. An example is shown in figure.

Cite As

suriyanath (2021). RSA algorithm (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)


In the higher version,you should use randi to replace randint ,and modify the position of the input varible.
my version is 2020a
e=randint(1,1,[2 n1]);
e=randi([2 n1],1,1);


No funciona. same to me.
Error in rsa_code (line 15)
e=randi(1,1,[2 n1]);

Juan David Peña Melo

No funciona.

Error in rsa_code (line 15)
e=randi(1,1,[2 n1]);

Islam Adam


How do I use this code to encrypt files?

dahmouni lyes

fang zheng

M. Arif Rahman

thank you..

Tim Kaufmann

missing a function, is this in an additional toolbox or base matlab is ok.
I'm using 2015b.

Undefined function or variable

Antonio Afta

khurria zafar


you should use extended eucleudianalgorithm to search for d.
this implementation is incorrect
dec(enc(msg)) != msg

jafar hassanpour

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!