You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
2 months ago
|
using System;
|
||
|
|
||
|
using Org.BouncyCastle.Crypto.Parameters;
|
||
|
using Org.BouncyCastle.Math;
|
||
|
|
||
|
namespace Org.BouncyCastle.Crypto.Generators
|
||
|
{
|
||
|
/**
|
||
|
* a Diffie-Hellman key pair generator.
|
||
|
*
|
||
|
* This generates keys consistent for use in the MTI/A0 key agreement protocol
|
||
|
* as described in "Handbook of Applied Cryptography", Pages 516-519.
|
||
|
*/
|
||
|
public class DHKeyPairGenerator
|
||
|
: IAsymmetricCipherKeyPairGenerator
|
||
|
{
|
||
|
private DHKeyGenerationParameters param;
|
||
|
|
||
|
public virtual void Init(
|
||
|
KeyGenerationParameters parameters)
|
||
|
{
|
||
|
this.param = (DHKeyGenerationParameters)parameters;
|
||
|
}
|
||
|
|
||
|
public virtual AsymmetricCipherKeyPair GenerateKeyPair()
|
||
|
{
|
||
|
DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
|
||
|
DHParameters dhp = param.Parameters;
|
||
|
|
||
|
BigInteger x = helper.CalculatePrivate(dhp, param.Random);
|
||
|
BigInteger y = helper.CalculatePublic(dhp, x);
|
||
|
|
||
|
return new AsymmetricCipherKeyPair(
|
||
|
new DHPublicKeyParameters(y, dhp),
|
||
|
new DHPrivateKeyParameters(x, dhp));
|
||
|
}
|
||
|
}
|
||
|
}
|