Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Javascript RSA implementation doesn't generate correctly

The problem is at generating either e or d. But what? The math seems good to me.

e > 1 and coprime to ϕ

d satisfies de≡1(modϕ(n))

Please help

var m = 4, //Message
    p = 13
    q = 7

    n = p * q
    T = (q - 1) * (p - 1)
    d = new Number;


//Calculate e:
for (let i = 2; i < T; i++) {

    if( (gcd(i,T) == 1) ) { var e = i; break; }
}

//Calculate d:
while ( e * d % T != 1 ) d++;

function gcd(k, n) {
    return k ? gcd(n % k, k) : n;
}


var e_m = m ** e % n,      // To encrypt
    d_m = e_m ** d % n    // To decrypt

console.log(`m: ${m}\np: ${p}\nq: ${q}\nn: ${n}\nT: ${T}\n\ne: ${e}\nd: ${d}\n\nencrypted: ${e_m}\ndecrypted: ${d_m}`);

//Results:
// encrypted: 23
// decrypted: 28 (not 4)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!