ElGamalParametersGenerator.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  8. {
  9. public class ElGamalParametersGenerator
  10. {
  11. private int size;
  12. private int certainty;
  13. private SecureRandom random;
  14. public void Init(
  15. int size,
  16. int certainty,
  17. SecureRandom random)
  18. {
  19. this.size = size;
  20. this.certainty = certainty;
  21. this.random = random;
  22. }
  23. /**
  24. * which Generates the p and g values from the given parameters,
  25. * returning the ElGamalParameters object.
  26. * <p>
  27. * Note: can take a while...
  28. * </p>
  29. */
  30. public ElGamalParameters GenerateParameters()
  31. {
  32. //
  33. // find a safe prime p where p = 2*q + 1, where p and q are prime.
  34. //
  35. BigInteger[] safePrimes = DHParametersHelper.GenerateSafePrimes(size, certainty, random);
  36. BigInteger p = safePrimes[0];
  37. BigInteger q = safePrimes[1];
  38. BigInteger g = DHParametersHelper.SelectGenerator(p, q, random);
  39. return new ElGamalParameters(p, g);
  40. }
  41. }
  42. }
  43. #pragma warning restore
  44. #endif