static public byte[] CalculateSharedKey(ECPrivateKeyParameters myKey, ECPublicKeyParameters sharedKey) { ECDHCBasicAgreement agreement = new ECDHCBasicAgreement(); agreement.Init(myKey); return(agreement.CalculateAgreement(sharedKey).ToByteArray()); }
public void TestECDHBasicAgreement() { SecureRandom random = new SecureRandom(); BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); FpCurve curve = new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b n, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G n, BigInteger.One); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, random); pGen.Init(genParam); AsymmetricCipherKeyPair p1 = pGen.GenerateKeyPair(); AsymmetricCipherKeyPair p2 = pGen.GenerateKeyPair(); // // two way // IBasicAgreement e1 = new ECDHBasicAgreement(); IBasicAgreement e2 = new ECDHBasicAgreement(); e1.Init(p1.Private); e2.Init(p2.Private); BigInteger k1 = e1.CalculateAgreement(p2.Public); BigInteger k2 = e2.CalculateAgreement(p1.Public); if (!k1.Equals(k2)) { Fail("calculated agreement test failed"); } // // two way // e1 = new ECDHCBasicAgreement(); e2 = new ECDHCBasicAgreement(); e1.Init(p1.Private); e2.Init(p2.Private); k1 = e1.CalculateAgreement(p2.Public); k2 = e2.CalculateAgreement(p1.Public); if (!k1.Equals(k2)) { Fail("calculated agreement test failed"); } }
public byte[] GetSharedSecretValue(ECPublicKeyParameters publicKey, ECPrivateKeyParameters privateKey) { ECDHCBasicAgreement eLacAgreement = new ECDHCBasicAgreement(); eLacAgreement.Init(privateKey); BigInteger eLA = eLacAgreement.CalculateAgreement(publicKey); byte[] eLABytes = eLA.ToByteArray(); return(eLABytes); }
public bool HasTestPassed(AsymmetricCipherKeyPair kp) { ECDHCBasicAgreement agreement = new ECDHCBasicAgreement(); agreement.Init(kp.Private); BigInteger agree1 = agreement.CalculateAgreement(kp.Public); AsymmetricCipherKeyPair testKP = getTestKeyPair(kp); agreement.Init(testKP.Private); BigInteger agree2 = agreement.CalculateAgreement(testKP.Public); agreement.Init(kp.Private); BigInteger agree3 = agreement.CalculateAgreement(testKP.Public); agreement.Init(testKP.Private); BigInteger agree4 = agreement.CalculateAgreement(kp.Public).Multiply(BigInteger.ValueOf(FipsKats.Values[FipsKats.Vec.ECDHKeyPairConsistencyVec][0])); return(!agree1.Equals(agree2) && !agree1.Equals(agree3) && agree3.Equals(agree4)); }
public static byte[] simetricKey(string pvtKey, string pubKey) { AsymmetricKeyParameter publicKey = (ECPublicKeyParameters)importKey(ReadOnlyParam.PemWritter.beginPublic + pubKey + ReadOnlyParam.PemWritter.endPublic); AsymmetricKeyParameter privateKey = ((AsymmetricCipherKeyPair)importKey(ReadOnlyParam.PemWritter.beginPrivate + pvtKey + ReadOnlyParam.PemWritter.endPrivate)).Private; ECDHCBasicAgreement acAgreement = new ECDHCBasicAgreement(); acAgreement.Init(privateKey); BigInteger a = acAgreement.CalculateAgreement(publicKey); byte[] sharedSecret = a.ToByteArray(); ECDHKekGenerator egH = new ECDHKekGenerator(DigestUtilities.GetDigest(ReadOnlyParam.sha)); egH.Init(new DHKdfParameters(NistObjectIdentifiers.Aes, sharedSecret.Length, sharedSecret)); byte[] symmetricKey = new byte[DigestUtilities.GetDigest(ReadOnlyParam.sha).GetDigestSize()]; egH.GenerateBytes(symmetricKey, 0, symmetricKey.Length); return(symmetricKey); }
public static Byte[] getSharedSecret(Byte[] PrivateKeyIn, Byte[] PublicKeyIn) { ECDHCBasicAgreement agreement = new ECDHCBasicAgreement(); X9ECParameters curve = null; ECDomainParameters ecParam = null; ECPrivateKeyParameters privKey = null; ECPublicKeyParameters pubKey = null; ECPoint point = null; curve = NistNamedCurves.GetByName("P-256"); ecParam = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed()); privKey = new ECPrivateKeyParameters(new BigInteger(PrivateKeyIn), ecParam); point = ecParam.Curve.DecodePoint(PublicKeyIn); pubKey = new ECPublicKeyParameters(point, ecParam); agreement.Init(privKey); BigInteger secret = agreement.CalculateAgreement(pubKey); return(secret.ToByteArrayUnsigned()); }
static byte[] GetSharedSecretValue(AsymmetricCipherKeyPair asymmetricCipherKeyPair, AsymmetricCipherKeyPair asymmetricCipherKeyPairA, bool isEncrypt = true) { var eLacAgreement = new ECDHCBasicAgreement(); eLacAgreement.Init(asymmetricCipherKeyPair.Private); ECDHCBasicAgreement acAgreement = new ECDHCBasicAgreement(); acAgreement.Init(asymmetricCipherKeyPairA.Private); var eLA = eLacAgreement.CalculateAgreement(asymmetricCipherKeyPairA.Public); var a = acAgreement.CalculateAgreement(asymmetricCipherKeyPair.Public); if (eLA.Equals(a) && !isEncrypt) { return(eLA.ToByteArray()); } if (eLA.Equals(a) && isEncrypt) { return(a.ToByteArray()); } return(null); }
/// <summary> /// Handles the server DH reply message. /// </summary> /// <param name="hostKey">The host key.</param> /// <param name="serverExchangeValue">The server exchange value.</param> /// <param name="signature">The signature.</param> private void HandleServerEcdhReply(byte[] hostKey, byte[] serverExchangeValue, byte[] signature) { _serverExchangeValue = serverExchangeValue; _hostKey = hostKey; _signature = signature; var cordSize = (serverExchangeValue.Length - 1) / 2; var x = new byte[cordSize]; Buffer.BlockCopy(serverExchangeValue, 1, x, 0, x.Length); // first byte is format. should be checked and passed to bouncy castle? var y = new byte[cordSize]; Buffer.BlockCopy(serverExchangeValue, cordSize + 1, y, 0, y.Length); var c = (FpCurve)DomainParameters.Curve; var q = c.CreatePoint(new Org.BouncyCastle.Math.BigInteger(1, x), new Org.BouncyCastle.Math.BigInteger(1, y)); var publicKey = new ECPublicKeyParameters("ECDH", q, DomainParameters); var k1 = KeyAgreement.CalculateAgreement(publicKey); SharedKey = k1.ToByteArray().ToBigInteger2().ToByteArray().Reverse(); }
public byte[] GetSharedSecret(string LocalPrivateKey, string RemoteAccountId) { //var pvtKey = Base58Encoding.DecodePrivateKey(privateKey); //var kp = new Neo.Wallets.KeyPair(pvtKey); //return Base58Encoding.EncodeAccountId(kp.PublicKey.EncodePoint(false).Skip(1).ToArray()); //var curve = SecNamedCurves.GetByName("secp256k1"); var curve = SecNamedCurves.GetByName("secp256r1"); var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H); //byte[] pkbytes = Base58Encoding.DecodeWithCheckSum(LocalPrivateKey); byte[] pkbytes = Base58Encoding.DecodePrivateKey(LocalPrivateKey); var privateKeyParameters = new ECPrivateKeyParameters(new BigInteger(1, pkbytes), domain); var dh = new ECDHCBasicAgreement(); dh.Init(privateKeyParameters); //var publicKeyBytes = Base58Encoding.DecodeWithCheckSum(RemotePublicKey); var publicKeyBytes = Base58Encoding.DecodeAccountId(RemoteAccountId); //var q = curve.Curve.DecodePoint(publicKeyBytes); var q = curve.Curve.CreatePoint(new BigInteger(1, publicKeyBytes.Take(32).ToArray()), new BigInteger(1, publicKeyBytes.Skip(32).ToArray())); var publicKeyParameters = new ECPublicKeyParameters(q, domain); var sharedSecret = dh.CalculateAgreement(publicKeyParameters); using (var sha = SHA256.Create()) { var hash = sha.ComputeHash(sharedSecret.ToByteArray()); return(hash); } }