Exemplo n.º 1
0
        static public byte[] DiffieHellmanGetSecretKey(string publicKey, out string thisPublicKey)
        {
            var df = new ECDiffieHellmanCng(256);

            thisPublicKey = df.PublicKey.ToXmlString();
            return(df.DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromXmlString(publicKey)));
        }
        public static byte[] DeriveKeyMaterial(byte[] privateKey, byte[] otherPublicKey, CngAlgorithm hashAlgorithm)
        {
#if Mono
            throw new NotSupportedException();
#else
            using (CngKey ck = CngKey.Import(privateKey, CngKeyBlobFormat.Pkcs8PrivateBlob))
                using (ECDiffieHellmanCng ecdh = new ECDiffieHellmanCng(ck))
                {
                    ecdh.HashAlgorithm = hashAlgorithm;
                    return(ecdh.DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromXmlString(Encoding.ASCII.GetString(otherPublicKey))));
                }
#endif
        }
Exemplo n.º 3
0
    public static byte[] ECDHKeyExchange(Uri URL, string Endpoint = "")
    {
        byte[] key = default(byte[]);

        using (ECDiffieHellmanCng AsymAlgo = new ECDiffieHellmanCng())
        {
            var    publicKey = AsymAlgo.PublicKey.ToXmlString();
            byte[] r         = HttpPost(URL, Endpoint, Encoding.UTF8.GetBytes(publicKey));

            ECDiffieHellmanCngPublicKey peerPublicKey = ECDiffieHellmanCngPublicKey.FromXmlString(Encoding.UTF8.GetString(r));
            key = AsymAlgo.DeriveKeyMaterial(peerPublicKey);
        }
        return(key);
    }
Exemplo n.º 4
0
        public static byte[] ECDHKeyExchange(Uri URL, byte[] PSK, string Endpoint = "")
        {
            byte[] key = default(byte[]);

            using (ECDiffieHellmanCng AsymAlgo = new ECDiffieHellmanCng())
            {
                byte[] encryptedPublicKey = Encrypt(PSK, Encoding.UTF8.GetBytes(AsymAlgo.PublicKey.ToXmlString()));
                byte[] r = Comms.HttpPost(URL, Endpoint, encryptedPublicKey);

                string decryptedPeerPublicKey             = Encoding.UTF8.GetString(Decrypt(PSK, r));
                ECDiffieHellmanCngPublicKey peerPublicKey = ECDiffieHellmanCngPublicKey.FromXmlString(decryptedPeerPublicKey);
                key = AsymAlgo.DeriveKeyMaterial(peerPublicKey);
            }
            return(key);
        }
Exemplo n.º 5
0
        // TODO: Migrate to self implemented Diffie-Hellman Key Exchange
        // ECDiffieHellmanCng is only available under Windows
        public static byte[] KeyExchange(Uri url)
        {
            byte[] key = default;

            using (ECDiffieHellmanCng AsymAlgo = new ECDiffieHellmanCng())
            {
                var    publicKey = AsymAlgo.PublicKey.ToXmlString();
                byte[] response  = Http.Post(url, Encoding.UTF8.GetBytes(publicKey));

                ECDiffieHellmanCngPublicKey peerPublicKey =
                    ECDiffieHellmanCngPublicKey.FromXmlString(Encoding.UTF8.GetString(response));
                key = AsymAlgo.DeriveKeyMaterial(peerPublicKey);
            }

            return(key);
        }
Exemplo n.º 6
0
        internal void DeriveKey(string otherPartyPublicKey)
        {
            aes         = new RijndaelManaged();
            aes.Padding = PaddingMode.PKCS7;

            try
            {
                byte[] aesBytes = dh.DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromXmlString(otherPartyPublicKey));

                for (int i = 0; i < aes.Key.Length; ++i)
                {
                    aes.Key[i] = aesBytes[i];
                }
                for (int i = 0; i < aes.IV.Length; ++i)
                {
                    aes.IV[i] = aesBytes[i];
                }
            }
            catch (Exception)
            {
                aes = null;
                throw;
            }
        }
Exemplo n.º 7
0
        static public byte[] DiffieHellmanGetSecretKey(string publicKey, CngKey otherDFInstanceKey)
        {
            var df = new ECDiffieHellmanCng(otherDFInstanceKey);

            return(df.DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromXmlString(publicKey)));
        }