This is a simple implementation of the RSA algorithm in TypeScript by John Gonsalves. The implementation includes key generation, encryption, decryption, and digital signature functionalities.
- Node.js (v12.0.0 or higher)
- npm (Node Package Manager)
- Clone or download this repository
- Install the dependencies:
npm installThe repository includes a test file (test.ts) that demonstrates both RSA encryption/decryption and digital signatures with example values.
To run the tests:
npx tsc && node dist/test.jsYou can modify the test values in test.ts to try different numbers. Keep in mind:
pandqshould be prime numberseshould be coprime with (p-1)(q-1)- The message should be a positive number less than n (where n = p * q)
The implementation includes several key functions:
gcd: Calculates the Greatest Common Divisor using the Euclidean algorithmmultInverse: Calculates the Multiplicative Inverse using the Extended Euclidean algorithmkeySchedule: Generates public and private key pairsrsaEncrypt: Encrypts a message using the public keyrsaDecrypt: Decrypts a ciphertext using the private keyRSA: Main function that demonstrates the complete encryption/decryption processRSA_DIGITAL_SIGNATURE: Function that demonstrates digital signature creation and verification
The implementation uses BigInt for handling large number calculations, which is necessary for real-world RSA operations.
The implementation includes a digital signature function that:
- Creates a signature by encrypting a message with the private key
- Verifies the signature by decrypting it with the public key
- Compares the result with the original message to verify