我正尝试使用弹性城堡.net库来执行Diffie-Hellman密钥交换,并在生成DHParams对象时遇到问题。Bouncy Castle Diffie-Hellman DHParams问题
我的解决方案将包括一个中央权威机构,它将为每个连接的客户端生成一个单独的DH密钥/对。这个想法是,我将为每个连接的客户端保留一个单独的DH密钥协议。然后,我将把p,g值发送给客户端,客户端将计算它的dh密钥协议。我想为每个客户端生成不同的p,g值。我正在使用BigInteger,但遇到了一些问题。
当我尝试创建一个新的对象DHParameters它抛出以下异常,每当我用比其他768位长:
System.ArgumentException was unhandled
Message="generator must in the range [2, p - 2]\r\nParameter name: g"
Source="BouncyCastle.Crypto"
ParamName="g"
StackTrace:
at Org.BouncyCastle.Crypto.Parameters.DHParameters..ctor(BigInteger p, BigInteger g, BigInteger q, Int32 m, Int32 l, BigInteger j, DHValidationParameters validation)
at Org.BouncyCastle.Crypto.Parameters.DHParameters..ctor(BigInteger p, BigInteger g, BigInteger q, Int32 l)
at TestDH.Program.Main(String[] args) in C:\dev\source\TestDH\TestDH\Program.cs:line 30
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
"generator must in the range [2, p - 2]"
我不知道,如果它的问题,但我已经试过各种在BigInteger构造函数中确定不同的值。
这里是我的代码:
SecureRandom sr = new SecureRandom();
// p,g generation, done by central authority
BigInteger g512 = new BigInteger(512, 30, sr);
BigInteger p512 = new BigInteger(512, 30, sr);
// p,g is then sent to client from central authority
// common - performed by both server and client sides
IAsymmetricCipherKeyPairGenerator keyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
DHParameters dhParams = new DHParameters(p512, g512, null, 512); // Here is where I get the exception if the first parameter if BigInteger is not 768 or lager
的问题是,它的时间太长,产生768位的素数 - 上没有其他进程运行的双核2.1GHz的处理器向上的5秒。对于每个启动连接的客户端而言,这都会带来很大的代价。我想为BigInteger使用较小的比特长度。
我可能这样做完全错误 - 关于如何使用弹性城堡执行DH以及测试/示例与我的使用案例不匹配,文档很少。我不想预先生成p,g值。
编辑 看来即使是768位长度也会在一段时间内出错。重新启动我的机器后,除1024以外的任何位都可以工作,即使这样也只有大约80%的时间。我觉得我做错了什么。