what-the-bank/sources/org/bouncycastle/crypto/ec/ECElGamalEncryptor.java

55 lines
2.5 KiB
Java

package org.bouncycastle.crypto.ec;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.math.ec.ECMultiplier;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
/* loaded from: classes6.dex */
public class ECElGamalEncryptor implements ECEncryptor {
private ECPublicKeyParameters key;
private SecureRandom random;
@Override // org.bouncycastle.crypto.ec.ECEncryptor
public void init(CipherParameters cipherParameters) {
SecureRandom secureRandom;
if (cipherParameters instanceof ParametersWithRandom) {
ParametersWithRandom parametersWithRandom = (ParametersWithRandom) cipherParameters;
if (!(parametersWithRandom.getParameters() instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption.");
}
this.key = (ECPublicKeyParameters) parametersWithRandom.getParameters();
secureRandom = parametersWithRandom.getRandom();
} else {
if (!(cipherParameters instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("ECPublicKeyParameters are required for encryption.");
}
this.key = (ECPublicKeyParameters) cipherParameters;
secureRandom = new SecureRandom();
}
this.random = secureRandom;
}
@Override // org.bouncycastle.crypto.ec.ECEncryptor
public ECPair encrypt(ECPoint eCPoint) {
ECPublicKeyParameters eCPublicKeyParameters = this.key;
if (eCPublicKeyParameters == null) {
throw new IllegalStateException("ECElGamalEncryptor not initialised");
}
ECDomainParameters parameters = eCPublicKeyParameters.getParameters();
BigInteger generateK = ECUtil.generateK(parameters.getN(), this.random);
ECPoint[] eCPointArr = {createBasePointMultiplier().multiply(parameters.getG(), generateK), this.key.getQ().multiply(generateK).add(eCPoint)};
parameters.getCurve().normalizeAll(eCPointArr);
return new ECPair(eCPointArr[0], eCPointArr[1]);
}
protected ECMultiplier createBasePointMultiplier() {
return new FixedPointCombMultiplier();
}
}