| RSA Encryption/Decryption Javascript -- By Monty | ![]() |
|
ENCRYPTION
Alice starts by picking two giant prime numbers, in the script they are named p and q. These are kept secret and are vital in the decryption later on. I have used 13 and 17 resprectively throughout the script. These are multiplied Alice's together to make a new number, named N. This is made public along with another number of her choice, named e.
So to recap, we now have:
So Bob wants to send an encrypted message to Alice... First he would look up her N and e in the directory of public keys and then he would put them into the following formula (in javascript)... var enc = (Math.pow(letter.charCodeAt(0),e))%N
So the character M is ASCII code 77, which will produce 155 as the ciphertext. View the source and check out the Encryption function and feel free to play around with the inputs.
DECRYPTION Alice receives her encrypted message from Bob (155). She now requires knowledge of her secret p and q to create a decryption key (or private key), named d. The decrpytion key is calculated with the following formula and a bit of clever maths... e*d = 1(mod(p-1)*(q-1))
The maths required to get to d is Euclid's Extended Algorithm, but I will not explain it here or anywhere in fact, as maths is not my strong point and I don't really understand the algorithm fully myself! This is the site from which I used the javascript for the Euclid's Extended Algorithm part of my script.
In brief, the RSA Algorithm's security comes from the difficulty of factoring large numbers, but here is the algorithm in action, but on a small scale!
Hope you find it useful and fun - Monty 23/08/04
|