1 package civitas.crypto.ciphertext;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Controller;
5
6 import civitas.crypto.CryptoBase;
7 import civitas.crypto.msg.CryptMessage;
8 import civitas.crypto.parameters.ElGamalParameters;
9 import civitas.crypto.publickey.ElGamalPublicKey;
10 import civitas.crypto.reencryptfactor.ElGamalReencryptFactor;
11 import civitas.util.CivitasBigInteger;
12
13 @Controller
14 public class ElGamalEncrypt {
15 @Autowired
16 CryptoBase cryptoBase;
17
18 public ElGamalCiphertext apply(final ElGamalPublicKey key, final CryptMessage msg) {
19 ElGamalParameters ps = key.params;
20 CivitasBigInteger m = msg.m();
21 CivitasBigInteger r = cryptoBase.generateRandomElement(ps.q);
22 CivitasBigInteger a = ps.g.modPow(r, ps.p);
23 CivitasBigInteger b = m.modMultiply(key.y.modPow(r, ps.p), ps.p);
24 return new ElGamalCiphertext(a, b);
25 }
26
27 public ElGamalCiphertext apply(
28 final ElGamalPublicKey key, final CryptMessage msg, final ElGamalReencryptFactor encryptFactor) {
29 ElGamalParameters ps = key.params;
30 CivitasBigInteger r = encryptFactor.r();
31 CivitasBigInteger m = msg.m();
32 CivitasBigInteger a = ps.g.modPow(r, ps.p);
33 CivitasBigInteger s = key.y.modPow(r, ps.p);
34 CivitasBigInteger b = m.modMultiply(s, ps.p);
35 return new ElGamalCiphertext(a, b);
36 }
37 }