DHBasicKeyPairGenerator.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 basic Diffie-Hellman key pair generator.
  9. *
  10. * This generates keys consistent for use with the basic algorithm for
  11. * Diffie-Hellman.
  12. */
  13. public class DHBasicKeyPairGenerator
  14. : IAsymmetricCipherKeyPairGenerator
  15. {
  16. private DHKeyGenerationParameters param;
  17. public virtual void Init(
  18. KeyGenerationParameters parameters)
  19. {
  20. this.param = (DHKeyGenerationParameters)parameters;
  21. }
  22. public virtual AsymmetricCipherKeyPair GenerateKeyPair()
  23. {
  24. DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
  25. DHParameters dhp = param.Parameters;
  26. BigInteger x = helper.CalculatePrivate(dhp, param.Random);
  27. BigInteger y = helper.CalculatePublic(dhp, x);
  28. return new AsymmetricCipherKeyPair(
  29. new DHPublicKeyParameters(y, dhp),
  30. new DHPrivateKeyParameters(x, dhp));
  31. }
  32. }
  33. }
  34. #endif