ElGamalKeyPairGenerator.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. using Org.BouncyCastle.Math;
  5. namespace Org.BouncyCastle.Crypto.Generators
  6. {
  7. /**
  8. * a ElGamal key pair generator.
  9. * <p>
  10. * This Generates keys consistent for use with ElGamal as described in
  11. * page 164 of "Handbook of Applied Cryptography".</p>
  12. */
  13. public class ElGamalKeyPairGenerator
  14. : IAsymmetricCipherKeyPairGenerator
  15. {
  16. private ElGamalKeyGenerationParameters param;
  17. public void Init(
  18. KeyGenerationParameters parameters)
  19. {
  20. this.param = (ElGamalKeyGenerationParameters) parameters;
  21. }
  22. public AsymmetricCipherKeyPair GenerateKeyPair()
  23. {
  24. DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
  25. ElGamalParameters egp = param.Parameters;
  26. DHParameters dhp = new DHParameters(egp.P, egp.G, null, 0, egp.L);
  27. BigInteger x = helper.CalculatePrivate(dhp, param.Random);
  28. BigInteger y = helper.CalculatePublic(dhp, x);
  29. return new AsymmetricCipherKeyPair(
  30. new ElGamalPublicKeyParameters(y, egp),
  31. new ElGamalPrivateKeyParameters(x, egp));
  32. }
  33. }
  34. }
  35. #endif