W
W
Wasya UK2019-11-02 18:24:12
JavaScript
Wasya UK, 2019-11-02 18:24:12

Why is RSA not decrypted?

I'm trying to figure out encryption. Everything seems to work, but only if you do not convert the numbers to ASCII, but as soon as the number is greater than 9, I get the wrong result.

const p = 3, q = 11; // simple numbers
const n = p * q; // simple numbers summ
const e = Math.floor(Math.random() * EilerFunc(p, q) + 1); // public key
const d = Math.round(EilerFunc(p, q) / e);
const message = "3125";

function EilerFunc(p, q) {
  return ((p - 1) * (q - 1));
}

function makeASCII(str) {
  return str.split('').map(char => char.charCodeAt()).join(' ');
}

function encrypt(str) {
  return str.split(' ').map(char => Math.floor(Math.pow(parseInt(char), e) % n)).join(' ');
}

function decrypt(str) {
  return str.split(' ').map(char => {
    let asciiSymbol = (Math.pow(parseInt(char), d) % n);
    
    return parseInt(String.fromCharCode(asciiSymbol));
  }).join(' ');
}


const asciiMessage = makeASCII(message);
const protected = encrypt(asciiMessage);
const decoded = decrypt(protected);

write("message:", message);
write("asciiMessage:", asciiMessage);
write("encrypt:", protected);
write("decrypt:", decoded);

function write() {
  document.querySelector('.content').innerHTML += [...arguments].join(' ') + '</br>';
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question