Sign packets

This commit is contained in:
jude
2023-03-04 14:19:26 +00:00
parent ab629a78b4
commit 96fe20503a
6 changed files with 80 additions and 31 deletions

View File

@ -1,4 +1,4 @@
import { generate_keypair } from "./paillier.js";
import { generate_rsa_keypair } from "./rsa.js";
import { generate_rsa_keypair, RsaPubKey } from "./rsa.js";
export { generate_keypair, generate_rsa_keypair };
export { generate_keypair, generate_rsa_keypair, RsaPubKey };

View File

@ -3,18 +3,29 @@ import { mod_exp, mod_inv } from "./math.js";
let p, q, pubKey, privKey;
class PubKey {
constructor(p, q) {
this.n = p * q;
this.e = 65537n;
export class RsaPubKey {
constructor(n, e) {
this.n = n;
this.e = e;
}
encrypt(m) {
return mod_exp(m, this.e, this.n);
}
toJSON() {
return {
n: "0x" + this.n.toString(16),
e: "0x" + this.e.toString(16),
};
}
static fromJSON(data) {
return new RsaPubKey(BigInt(data.n), BigInt(data.e));
}
}
class PrivKey {
class RsaPrivKey {
constructor(p, q) {
this.n = p * q;
this.d = mod_inv(65537n, (q - 1n) * (p - 1n));
@ -40,8 +51,8 @@ export function generate_rsa_keypair() {
q = BigInt(window.sessionStorage.getItem("rsa_q"));
}
pubKey = new PubKey(p, q);
privKey = new PrivKey(p, q);
pubKey = new RsaPubKey(p * q, 65537n);
privKey = new RsaPrivKey(p, q);
return { pubKey, privKey };
}