/rsa.js

RSA in JavaScript.

Primary LanguageJavaScript

RSA.js

This is a simple javascript implementation of the RSA protocol.

Usage

Make sure you are located in the root directory of the repo.

Key generation

node index.js keyGen bitLength

Example:

node index.js keyGen 200

Output:

{ privKey:
   16954976399285438921794663501569442589369381262668438613172567451479356170826263602719687287439000294872841092754072245n,
  pubKey: 65537n,
  modulus:
   248520883053633237548214156313197128038058927158050052870204426013162831563616350936880148646497325348182465415128971071n }

Encryption

node index.js encrypt message pubKey modulus

Example:

node index.js encrypt "Hello World" 65537n 248520883053633237548214156313197128038058927158050052870204426013162831563616350936880148646497325348182465415128971071n

Output:

228330125306521016027408724198298194464643672889066840642634584098887056086731732761890355185770132374033716999795886420n

Decryption

node index.js decrypt encryptedMessage privKey modulus

Example:

node index.js decrypt 228330125306521016027408724198298194464643672889066840642634584098887056086731732761890355185770132374033716999795886420n 16954976399285438921794663501569442589369381262668438613172567451479356170826263602719687287439000294872841092754072245n 248520883053633237548214156313197128038058927158050052870204426013162831563616350936880148646497325348182465415128971071n

Output:

Hello World

References

Theory

RSA wiki
Miller-Rabin wiki
Modular exponentiation wiki
Integer square root wiki
Newton method wiki
Extended Euclidean Algorithm wiki
Euclidean algorithm wiki
ASCII wiki

JavaScript

JS BigInt
JS BigInt guide
Destructuring assignment

Useful tools

First 10000 primes
BigPrimes
Decimal-to-binary