@stellot/crypto
Version:
Crypto libraries for front and backend
47 lines (46 loc) • 1.98 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsbn_1 = require("jsbn");
const Errors = __importStar(require("./errors"));
const utils_1 = require("./utils");
const encryption_1 = __importDefault(require("./encryption"));
const Utils = __importStar(require("./utils"));
class DecryptionElGamal extends encryption_1.default {
constructor(p, g, y, x) {
super(p, g, y);
this.x = utils_1.parseBigInt(x);
}
decrypt(m) {
if (!this.x)
throw new Errors.MissingPrivateKeyError();
const p = this.p;
const r = Utils.getRandomBigInt(Utils.BIG_TWO, this.p.subtract(jsbn_1.BigInteger.ONE));
const aBlind = this.g.modPow(r, p).multiply(m.a).remainder(p);
const ax = aBlind.modPow(this.x, p);
const plaintextBlind = ax.modInverse(p).multiply(m.b).remainder(p);
return this.y.modPow(r, p).multiply(plaintextBlind).remainder(p);
}
}
exports.default = DecryptionElGamal;