43 lines
885 B
JavaScript
43 lines
885 B
JavaScript
let p, q, pubKey, privKey;
|
|
|
|
class PubKey {
|
|
constructor(p, q) {
|
|
this.n = p * q;
|
|
this.g = this.n + 1n;
|
|
}
|
|
|
|
encrypt(m) {
|
|
// Compute g^m r^n mod n^2
|
|
let r = this.n;
|
|
|
|
while (r >= n) {
|
|
r = random2048();
|
|
}
|
|
|
|
// Compute g^m by binomial theorem.
|
|
let gm = 1n + this.n * m;
|
|
// Compute g^m r^n from fact that g^n = 1
|
|
return fastModularExponentiation(gm * r, this.n, this.n ** 2);
|
|
}
|
|
}
|
|
|
|
class PrivKey {
|
|
constructor(lambda, mu) {
|
|
this.lambda = lambda;
|
|
this.mu = mu;
|
|
}
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
return;
|
|
|
|
p = generate_prime();
|
|
q = generate_prime();
|
|
|
|
let n = p * q;
|
|
let lambda = (p - 1n) * (q - 1n);
|
|
|
|
pubKey = new PubKey(p, q);
|
|
privKey = new PrivKey(lambda, fastModularExponentiation(lambda, lambda - 1n, n));
|
|
});
|