Example #1
0
 public DiffieHellman(DiffieHellmanPublicKey publicKey, KeyAgreementKeyDerivationFunction kdFunc, KeyAgreementKeyDerivationHashAlgorithm kdHashAlgo)
     : base(kdFunc, kdHashAlgo)
 {
     _group      = DiffieHellmanGroupType.None;
     _keySize    = publicKey.KeySize;
     _p          = publicKey.P;
     _g          = publicKey.G;
     _privateKey = GeneratePrivateKey(_p);
 }
        public DiffieHellman(DiffieHellmanPublicKey publicKey, KeyAgreementKeyDerivationFunction kdFunc, KeyAgreementKeyDerivationHashAlgorithm kdHashAlgo)
            : base(kdFunc, kdHashAlgo)
        {
            _keySize = publicKey.KeySize;

            _p = publicKey.P;
            _g = publicKey.G;

            GeneratePrivateKey();
        }
Example #3
0
        protected override byte[] ComputeKey(byte[] otherPartyPublicKey)
        {
            DiffieHellmanPublicKey opPublicKey = new DiffieHellmanPublicKey(otherPartyPublicKey);

            if (opPublicKey.Group != _group)
            {
                throw new CryptoException("DiffieHellman group mismatch.");
            }

            if (opPublicKey.KeySize != _keySize)
            {
                throw new CryptoException("DiffieHellman key size mismatch.");
            }

            if (opPublicKey.P != _p)
            {
                throw new CryptoException("DiffieHellman public key parameter P doesn't match.");
            }

            if (opPublicKey.G != _g)
            {
                throw new CryptoException("DiffieHellman public key parameter G doesn't match.");
            }

            HashAlgorithm hash;

            switch (base.KeyDerivationHashAlgorithm)
            {
            case KeyAgreementKeyDerivationHashAlgorithm.SHA256:
                hash = HashAlgorithm.Create("SHA256");
                break;

            case KeyAgreementKeyDerivationHashAlgorithm.SHA384:
                hash = HashAlgorithm.Create("SHA384");
                break;

            case KeyAgreementKeyDerivationHashAlgorithm.SHA512:
                hash = HashAlgorithm.Create("SHA512");
                break;

            default:
                throw new CryptoException("Hash algorithm not supported.");
            }

            return(hash.ComputeHash(BigInteger.ModPow(opPublicKey.X, _privateKey, _p).ToByteArray()));
        }