我正在使用Bouncy城堡库来使用DH密钥交换。到目前为止,我已经尝试了很多,但是按键似乎不匹配。我究竟做错了什么?请帮助!
var p = Org.BouncyCastle.Math.BigInteger.ProbablePrime(1024, new Random());
var g = new Org.BouncyCastle.Math.BigInteger("5");
var importedParameters = new Org.BouncyCastle.Crypto.Parameters.DHParameters(p, g);
//---------------------bob---------------------------------------------------
var scrnd = new SecureRandom();
var bobkeyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
var kgp = new DHKeyGenerationParameters(scrnd, importedParameters);
bobkeyGen.Init(kgp);
var bobasypair = bobkeyGen.GenerateKeyPair();
var dhPublicKeyParameters = bobasypair.Public as DHPublicKeyParameters;
var dhPrivateKeyParameters = bobasypair.Private as DHPrivateKeyParameters;
var internalKeyAgree = AgreementUtilities.GetBasicAgreement("DH");
internalKeyAgree.Init(dhPrivateKeyParameters);
var secret = internalKeyAgree.CalculateAgreement(dhPublicKeyParameters);
//-----------------------------alice--------------------------------------------------------------------------------
var alicekeyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
var allkgp = new DHKeyGenerationParameters(scrnd, importedParameters);
alicekeyGen.Init(allkgp);
var aliceasypair = alicekeyGen.GenerateKeyPair();
var alicedhPublicKeyParameters = aliceasypair.Public as DHPublicKeyParameters;
var alicedhPrivateKeyParameters = aliceasypair.Private as DHPrivateKeyParameters;
var allinternalKeyAgree = AgreementUtilities.GetBasicAgreement("DH");
allinternalKeyAgree.Init(alicedhPrivateKeyParameters);
var tobesamesecret = allinternalKeyAgree.CalculateAgreement(alicedhPublicKeyParameters);
if (!secret.Equals(tobesamesecret))
throw new Exception();