An implementation of the RSA cryptosystem relying on the native JS (stage 3) implementation of BigInt. It can be used by any Web Browser or webview supporting BigInt and with Node.js (>=10.4.0). In the latter case, for multi-threaded primality tests, you should use Node.js v11 or newer or enable at runtime with node --experimental-worker
with Node.js version >= 10.5.0 and < 11.
The operations supported on BigInts are not constant time. BigInt can be therefore unsuitable for use in cryptography. Many platforms provide native support for cryptography, such as Web Cryptography API or Node.js Crypto.
The RSA cryptosystem, named after and invented by Rivest–Shamir–Adleman in 1977. In such a cryptosystem, the encryption key is public and distinct from the decryption key which is kept secret (private). In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the "factoring problem".
- Define the bit length of the modulus
n
, orkeyLength
in bits. - Choose two large prime numbers
p
andq
randomly and independently of each other such thatgcd( p·q, (p-1)(q-1) )=1
andn=p·q
has a key length of keyLength. For instance:- Generate a random prime
p
with a bit length ofkeyLength/2 + 1
. - Generate a random prime
q
with a bit length ofkeyLength/2
. - Repeat until the bitlength of
n=p·q
iskeyLength
.
- Generate a random prime
- Compute Euler's totient function such that
phi(n) = (p-1)*(q-1)
. - Choose a comprime of
phi(n)
as the public exponente
:- Generate a simple
BigInt
number. Often is a predifined primee = 65537 = 2^16 + 1
- Generate a simple
- Compute the private exponent
d
asd = e^-1 mod(phi(n))
.
The public (encryption) key is (e, n).
The private (decryption) key is (d, n).
Let m
be the clear-text message,
- Compute ciphertext as:
c = m^e mod(n)
Let c
be the ciphertext to decrypt.
- Compute the plaintext message as:
m = c^d mod(n)
npm install bigint-crypto-utils
npm install bigint-conversion