Skip to content
  • Sponsor TheAlgorithms/JavaScript

  • Notifications You must be signed in to change notification settings
  • Fork 5.7k

feat: add RSA encryption and decryption algorithm #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Ciphers/RSAAlgorithm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// RSAAlgorithm.js

/**
* Generates the greatest common divisor of two numbers.
* @param {number} a - First number.
* @param {number} b - Second number.
* @returns {number} - The GCD of a and b.
*/
export function gcd(a, b) {
if (b === 0) {
return a
}
return gcd(b, a % b)
}

/**
* Calculates modular inverse using Extended Euclidean Algorithm.
* @param {number} e - The number to find inverse for.
* @param {number} phi - The modulus.
* @returns {number} - The modular inverse of e mod phi.
*/
export function modInverse(e, phi) {
let [m0, x0, x1] = [phi, 0, 1]

if (phi === 1) {
return 0
}

while (e > 1) {
let q = Math.floor(e / phi)
;[e, phi] = [phi, e % phi]
;[x0, x1] = [x1 - q * x0, x0]
}

if (x1 < 0) {
x1 += m0
}

return x1
}

/**
* Performs modular exponentiation.
* @param {number} base - Base number.
* @param {number} exponent - Exponent.
* @param {number} modulus - Modulus.
* @returns {number} - (base^exponent) % modulus.
*/
export function modPow(base, exponent, modulus) {
if (modulus === 1) return 0
let result = 1
base = base % modulus

while (exponent > 0) {
if (exponent % 2 === 1) {
result = (result * base) % modulus
}
exponent = Math.floor(exponent / 2)
base = (base * base) % modulus
}

return result
}

/**
* Generates RSA keys.
* @param {number} p - A prime number.
* @param {number} q - A prime number.
* @returns {{publicKey: {e: number, n: number}, privateKey: {d: number, n: number}}}
*/
export function generateKeys(p, q) {
const n = p * q
const phi = (p - 1) * (q - 1)

let e = 2
while (e < phi && gcd(e, phi) !== 1) {
e++
}

const d = modInverse(e, phi)

return {
publicKey: { e, n },
privateKey: { d, n }
}
}

/**
* Encrypts a message with a public key.
* @param {number} message - The message to encrypt (as a number).
* @param {{e: number, n: number}} publicKey - The public key.
* @returns {number} - The encrypted message.
*/
export function encrypt(message, publicKey) {
return modPow(message, publicKey.e, publicKey.n)
}

/**
* Decrypts a cipher with a private key.
* @param {number} cipher - The encrypted message (cipher).
* @param {{d: number, n: number}} privateKey - The private key.
* @returns {number} - The decrypted message.
*/
export function decrypt(cipher, privateKey) {
return modPow(cipher, privateKey.d, privateKey.n)
}
14 changes: 14 additions & 0 deletions Ciphers/test/RSAAlgorithm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RSAAlgorithm.test.js
import { describe, it, expect } from 'vitest'
import { generateKeys, encrypt, decrypt } from '../RSAAlgorithm.js'

describe('RSA Algorithm', () => {
it('should encrypt and decrypt correctly', () => {
const { publicKey, privateKey } = generateKeys(61, 53) // two primes
const message = 65
const cipher = encrypt(message, publicKey)
const decrypted = decrypt(cipher, privateKey)

expect(decrypted).toBe(message)
})
})

Unchanged files with check annotations Beta

* like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2"
*
* But the catch is, we have to check only if the alphanumeric characters
* are palindrome i.e remove spaces, symbols, punctuations etc

Check failure on line 14 in String/AlphaNumericPalindrome.js

GitHub Actions / Check for spelling errors

punctuations ==> punctuation, punctuation's
* and the case of the characters doesn't matter
*/
const alphaNumericPalindrome = (str) => {
* A Subsequence is sequence obtained by deleting some or no elements without changing the order of elements
* Example: Given a string = "abcd"
* 1. "abc" is a subsequence
* 2. "abd" is a subsequence

Check failure on line 8 in Recursive/SubsequenceRecursive.js

GitHub Actions / Check for spelling errors

abd ==> and, bad
* 3. But "ba" is not a subsequence (because order is changed)
*
* What is lexicographical order?
* In simple terms, lexicographical order is dictionary order.
* Example: Given a string = "abcd"
* 1. "abc" will come before "abcd".
* 2. "abd" will come before "ac".

Check failure on line 15 in Recursive/SubsequenceRecursive.js

GitHub Actions / Check for spelling errors

abd ==> and, bad
*
* References for meaning of subsequence & lexicographical:
* https://en.wikipedia.org/wiki/Subsequence