Crunch RSA Example

RSA is one of the first practicable public-key cryptosystems. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers.

In this example, the Crunch arbitrary percision arithmetic library is used to generate an RSA key-pair, and uses it to "encrypt" and "decrypt" data.

Toggle base-10 integer representation

Enable base-10 integer representation in the fields below, to more easily visualize the numbers. This adds 10-20 milliseconds to each operation.

Generate RSA Key-Pair

Select a key size and click Generate. Larger key sizes will take longer to generate. Key material will populate the fields below.

Private Key Material

crunch.nextPrime(random N bit number) crunch.nextPrime(random N bit number) crunch.mul(crunch.decrement(p), crunch.decrement(q)) crunch.inv(e, f) crunch.inv(p, q)

Public Key Material

crunch.nextPrime(random N bit number) crunch.mul(p, q)

Modular Exponentiation (Encyption/Decryption)

Once a key-pair is generated, you can test the encryption/decryption using the following fields. This actually does modular exponentiation, which is the basis of the RSA cryptosystem.

The encryption is executed via the function crunch.exp(x, e, n) and decryption via crunch.gar(x, p, q, d, u).

The calculations on this page are executed sequentially. Because Crunch is web-worker enabled, some of the above operations could be parallelized for even better performance.

All numbers on this page are represented by byte-arrays which represent arbitrary-precision integers.

« Back to Examples