/// <summary> /// Derives a shared secret key from a private key and another persons public key /// </summary> /// <param name="myPrivateKey">the private key which is used</param> /// <param name="otherPartyPublicKey">the public key of the other person</param> /// <returns></returns> public byte[] DeriveKey(byte[] myPrivateKey, byte[] otherPartyPublicKey) { DHPrivateKeyParameters privKey = null; try { privKey = (DHPrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(myPrivateKey); } catch (InvalidCastException exception) { string message = "Private Key Import Failed!\n" + $"{exception.Message}.\n" + "The contents of the source do not represent a valid DH key parameter\n" + "Verify that the key is not corrupted.\n" + "- or - Verify that the correct key is selected."; throw new CryptoException(message, exception); } var a1 = new DHAgreement(); a1.Init(privKey); BigInteger m1 = a1.CalculateMessage(); DHPublicKeyParameters pubKey = null; try { pubKey = (DHPublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(otherPartyPublicKey); } catch (InvalidCastException exception) { string message = "Public Key Import Failed!\n" + $"{exception.Message}.\n" + "The contents of the source do not represent a valid DH key parameter\n" + "Verify that the key is not corrupted.\n" + "- or - Verify that the correct key is selected."; throw new CryptoException(message, exception); } //Both party keys must share the same DHParameters to be able to calculate the agreement BigInteger k1 = a1.CalculateAgreement(pubKey, m1); return(k1.ToByteArrayUnsigned()); }
private void doTestDH( int size, BigInteger g, BigInteger p) { DHKeyPairGenerator kpGen = getDHKeyPairGenerator(g, p); // // generate first pair // AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu1 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters)pair.Private; // // generate second pair // pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu2 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters)pair.Private; // // two way // DHAgreement e1 = new DHAgreement(); DHAgreement e2 = new DHAgreement(); e1.Init(pv1); e2.Init(pv2); BigInteger m1 = e1.CalculateMessage(); BigInteger m2 = e2.CalculateMessage(); BigInteger k1 = e1.CalculateAgreement(pu2, m2); BigInteger k2 = e2.CalculateAgreement(pu1, m1); if (!k1.Equals(k2)) { Fail(size + " bit 2-way test failed"); } }
private void doTestGPWithRandom( DHKeyPairGenerator kpGen) { // // generate first pair // AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu1 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters)pair.Private; // // generate second pair // pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu2 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters)pair.Private; // // two way // DHAgreement e1 = new DHAgreement(); DHAgreement e2 = new DHAgreement(); e1.Init(new ParametersWithRandom(pv1, new SecureRandom())); e2.Init(new ParametersWithRandom(pv2, new SecureRandom())); BigInteger m1 = e1.CalculateMessage(); BigInteger m2 = e2.CalculateMessage(); BigInteger k1 = e1.CalculateAgreement(pu2, m2); BigInteger k2 = e2.CalculateAgreement(pu1, m1); if (!k1.Equals(k2)) { Fail("basic with random 2-way test failed"); } }