Home > Miscellany > RSA Demo

Javascript RSA Algorithm Demonstration.

I created this demonstration of the RSA algorithm for a course that I took about data structures and algorithms. Javascript has some limitations on handling large numbers - I compensated for some of these with the Extended Euclidean and Russian Peasant algorithms, but there are still some problems, depending on the choice of P and Q. The defaults values work. For more information on the RSA algorithm, follow the links at the end of the page.

Step 1:

Select two numbers which have no common factors. Call them P and Q. Since they have no common factors, they are said to be "relatively prime." You can enter your own values for P and Q, or use the default values provided.




N = P * Q =

M = (P - 1)(Q - 1) =



Step 2:

Select the public key, E to be relatively prime to M.


(This button calculates E for the value of M currently shown in the above textfield. Thus if you change M by typing in a new value after calculating it for the P and Q you entered, that change will be reflected here.)

E =



Step 3:

Select the private key, D, so that (D * E) % M = 1.


(This button calculates D for the values of E and M which are currently showing in the textfields above. Thus if you change E or M by typing in a new value, that change will be reflected here.)

D =



Step 4:

Encrypt and Decrypt your message. The message must be an integer with a magnitude less than (N-1).


Message B =

Encrypted message C =

Message C =
Decrypted message B =



Web Resources (for further information):

RSA Algorithm

Paj's Home: Cryptography: RSA Algorithm:
A page that explains the algorithm using clear language and in enough detail to be a useful starting point for a programmer.

Cyber RSA@Crypto.Center
A simulation in Javascript of the RSA algorithm, using 1 letter, and using a shortcut (that is not the Extended Euclidean Algorithm) to find D.

Extended Euclidean Algorithm

Extended Euclidean Algorithm
Clear, simple explanation of what it is.

Wilkipedia article on the Euclidean Algorithm and its extension
Explains the algorithm and provides javascript examples.

Art Lew's PDF File
Somewhat tough to read, but it gives the procedure for finding the modular inverse of two numbers using the EEA.

Russian Peasant Algorithm

Computation - Exponentiation via the Russian Peasant Algorithm
Provides code for the algorithm.

Ask Dr. Math: Russian Peasant Multiplication
Explains the multiplication process that forms the basic idea for the exponentiation algorithm.


Web Developers Virtual Library (WDVL): Javascript
Contains tutorials and links to resources.

Javascript FAQ
A handy reference.

Netscape's Online Documentation for Javascript
Has some useful information, including the Document Object Model (DOM)