UNPKG

@stellot/crypto

Version:

Crypto libraries for front and backend

21 lines (20 loc) 755 B
import { parseBigInt } from "./utils"; import { BigInteger as BigInt } from 'jsbn'; import * as Utils from "./utils"; import DecryptedValue from "./decryptedValue"; import EncryptedValue from "./encryptedValue"; export default class EncryptionElGamal { constructor(p, g, y) { this.p = parseBigInt(p); this.g = parseBigInt(g); this.y = parseBigInt(y); } encrypt(m, k) { const tmpKey = k ? Utils.parseBigInt(k) : Utils.getRandomBigInt(BigInt.ONE, this.p.subtract(BigInt.ONE)); const mBi = new DecryptedValue(m).bi; const p = this.p; const a = this.g.modPow(tmpKey, p); const b = this.y.modPow(tmpKey, p).multiply(mBi).remainder(p); return new EncryptedValue(a, b); } }