The answer is most definitely not.
This protocol depends on the integrity of the messages received.
One easy way to see how it can fail is if an active attacker can change the value
of ya to 1 and change the value of yb to 1,
then Alice and Bob will still agree on a key but that key would be 1 raised to their secret power,
which is still 1.
It would be the key value 1, which would be known to the eaves dropper
and make all messages encrypted using that key vulnerable.
The attacker could also intercept that protocol here
and separately execute the protocol with each party.
This would make Alice think that she has a secure key with Bob,
but it's actually a secure key shared with an attacker in the middle.
It would make Bob that that he has a secure key with Alice,
but it's actually a key shared with an attacker in the middle.
That means the attacker in the middle could take a message that Alice encrypts
with this key that's actually shared with the attacker in the middle,
can decrypt that message, can then re-encrypt that message
using the key that the attacker shared with Bob and send the result to Bob.
Bob will decrypt it, thinking it's a good message from Alice.
This is a very dangerous attack.
Diffie-Hellman can only be used in places where either the integrity of the channel is guaranteed
or there is a way for Bob to find out Alice's ya value and know that it's really Alice.
It could also be useful in the case where there's some trusted directory that has the y values.
Bob could look up Alice's ya value, know the q and the g values,
and then Bob would know that he's communicating with Alice if he trusts this directory.
One way to provide that is using a certificate authority,
which we'll talk about in unit 5.
