DHKeyPairGenerator.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  7. {
  8. /**
  9. * a Diffie-Hellman key pair generator.
  10. *
  11. * This generates keys consistent for use in the MTI/A0 key agreement protocol
  12. * as described in "Handbook of Applied Cryptography", Pages 516-519.
  13. */
  14. public class DHKeyPairGenerator
  15. : IAsymmetricCipherKeyPairGenerator
  16. {
  17. private DHKeyGenerationParameters param;
  18. public virtual void Init(
  19. KeyGenerationParameters parameters)
  20. {
  21. this.param = (DHKeyGenerationParameters)parameters;
  22. }
  23. public virtual AsymmetricCipherKeyPair GenerateKeyPair()
  24. {
  25. DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
  26. DHParameters dhp = param.Parameters;
  27. BigInteger x = helper.CalculatePrivate(dhp, param.Random);
  28. BigInteger y = helper.CalculatePublic(dhp, x);
  29. return new AsymmetricCipherKeyPair(
  30. new DHPublicKeyParameters(y, dhp),
  31. new DHPrivateKeyParameters(x, dhp));
  32. }
  33. }
  34. }
  35. #pragma warning restore
  36. #endif