I've tried to implement the basics of RSA into a simple NodeJS / javascript file.
For encrypting I've used c = m^e % n.
For decrypting I've used m = c^d % n.
const p = 7; // choice prime
const q = 13; // choice prime
const n = p * q;
const e = 5; // choice
const Ke = [e, n]; // the private key
const d = 29; // choice
const Kd = [d, n]; // the public key
let message = 'hello'
let encrypted = []
message = message.toUpperCase()
for (let i = 0; i < message.length; i++) {
const char = message.charCodeAt(i) - 64 // m
const pow = Math.pow(char, e) // m^e
const mod = pow % n // mod n
encrypted[i] = mod
}
encrypted.forEach(char => {
const pow = Math.pow(char, d) // c^d
const mod = pow % n // mod n
console.log(String.fromCharCode(mod + 64))
})
The encrypting goes well. However, the decrypting has some issues. It shows other characters then I've put in in the let message = 'hello' part
What am I doing wrong with decrypting?