public byte[] CalculateCommonSecret(EthECKey publicKey) { var agreement = new ECDHBasicAgreement(); agreement.Init(_ecKey.PrivateKey); var z = agreement.CalculateAgreement(publicKey._ecKey.GetPublicKeyParameters()); return BigIntegers.AsUnsignedByteArray(agreement.GetFieldSize(), z); }
public static byte[] CalculateECDHBasicAgreement(ECPublicKeyParameters publicKey, ECPrivateKeyParameters privateKey) { ECDHBasicAgreement eCDHBasicAgreement = new ECDHBasicAgreement(); eCDHBasicAgreement.Init(privateKey); BigInteger n = eCDHBasicAgreement.CalculateAgreement(publicKey); return(BigIntegers.AsUnsignedByteArray(eCDHBasicAgreement.GetFieldSize(), n)); }
/// <summary> /// Creates symmetric key from sender's public key and own key pair /// </summary> /// <param name="publicKeyBytes"></param> /// <returns></returns> protected byte[] GetSymmetricKey(byte[] publicKeyBytes) { ECPublicKeyParameters publicKey = new ECPublicKeyParameters(X9.Curve.DecodePoint(publicKeyBytes), EcSpec); var agree = new ECDHBasicAgreement(); agree.Init(KeyPair.Private); BigInteger z = agree.CalculateAgreement((AsymmetricKeyParameter)publicKey); return(BigIntegers.AsUnsignedByteArray(agree.GetFieldSize(), z)); }
public string GenerateSharedSecretHex(string pubKeyHex) { var pubKeyBytes = pubKeyHex.HexToByteArray(); var q = _curve.Curve.DecodePoint(pubKeyBytes); var remotePublicKey = new ECPublicKeyParameters("ECDH", q, _domain); var agreement = new ECDHBasicAgreement(); agreement.Init(_key.Private); var basicAgreementResult = agreement.CalculateAgreement(remotePublicKey); return(BigIntegers.AsUnsignedByteArray(agreement.GetFieldSize(), basicAgreementResult).ToHex(true)); }
public static byte[] CalculateECDHBasicAgreement(ECPublicKeyParameters publicKey, ECPrivateKeyParameters privateKey) { ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement(); basicAgreement.Init(privateKey); BigInteger agreementValue = basicAgreement.CalculateAgreement(publicKey); /* * RFC 4492 5.10. Note that this octet string (Z in IEEE 1363 terminology) as output by * FE2OSP, the Field Element to Octet String Conversion Primitive, has constant length for * any given field; leading zeros found in this octet string MUST NOT be truncated. */ return(BigIntegers.AsUnsignedByteArray(basicAgreement.GetFieldSize(), agreementValue)); }
public string GenerateSharedSecretHex(string pubKeyHex) { var encoded = pubKeyHex.HexToByteArray(); if (encoded.Length == 64) { var numArray = new byte[encoded.Length + 1]; numArray[0] = (byte)4; Array.Copy((Array)encoded, 0, (Array)numArray, 1, encoded.Length); encoded = numArray; } var publicKeyParameters = new ECPublicKeyParameters("ECDH", ECDH_Key._curve.Curve.DecodePoint(encoded), ECDH_Key._domain); var ecdhBasicAgreement = new ECDHBasicAgreement(); ecdhBasicAgreement.Init(_key.Private); return(BigIntegers.AsUnsignedByteArray(ecdhBasicAgreement.GetFieldSize(), ecdhBasicAgreement.CalculateAgreement((ICipherParameters)publicKeyParameters)).ToHex(true)); }