# RSA Cryptosystem Math

I wanted to learn more about the RSA Cryptosystem so I started reading more about the math behind it. So far I've been able to generate the following example in JavaScript. This uses pre-calculated values however the comments describe the rule behind arriving at the values.

``````var rsa_cryptosystem = {
//1. Select two distinct prime numbers p and q
p: 3,
q: 11,
//2. compute n as a product of pq
n: rsa_cryptosystem.p*rsa_cryptosystem.q,
//3. choose a variable z such that it is a co-prime to n
z: (rsa_cryptosystem.p-1)*(rsa_cryptosystem.q-1),
//4. choose variable pubK such that 1 < pubK < z and pubK is prime and co-prime to n
pubK: 7,
//5. choose privK such that (privK*e)%z = 1 and privK is prime
privK: 3,
encrypt: function(m) {
return Math.pow(m, rsa_cryptosystem.pubK)%rsa_cryptosystem.n;
},
decrypt: function(m) {
return Math.pow(m, rsa_cryptosystem.privK)%rsa_cryptosystem.n;
}
}
``````

## Observation

It appears the values that can be encrypted within this system are limited to the value used to calculate the modulus, in this case 33.

``````for(var i = 0; i < 1000; i++) {
var m = rsa_cryptosystem.encrypt(i);
if (i == rsa_cryptosystem.decrypt(m)) {
console.log('True %s', i);
} else {
console.log('False %s', i);
}
}
``````