Before describing it mathematically, I'm going to describe it
as an analogy to paint mixing, and this analogy comes from Simon Singh
from "The Code Book," which is a really fun book about the history of cryptography.
We have our usual suspects, Alice and Bob, that want to establish a shared key,
and they're going to do this by first picking a public color.
Let's say they both like yellow.
And they'll start with cans of identical yellow paint.
Both Alice and Bob start with a can of yellow paint.
Then each party will pick their own secret color.
Let's say Alice picks a greenish color,
and Bob picks a purplish color.
Then what they do is mix their secret colors with the public color.
They're going to mix those two, and the green and yellow
will make some ugly green color,
and Bob will do the same thing, mixing the yellow and purple
to get some new ugly color.
At this stage, they change colors.
Alice gives Bob the ugly green color that she got by mixing her secret color with yellow.
Bob gives Alice the ugly pinkish color he got by mixing yellow with his secret color.
Once they've received the other party's colors,
they mix their secret color with the other party's mixed color.
At this stage, both parties should have the same color.
Alice mixed her green with the pink that came from yellow and Bob's secret color,
and that will give some new color probably uglier than the grey that I'm drawing,
but Bob will get the same thing because Bob is mixing the yellow,
Alice's secret color, and Bob's secret color and getting the same color.
This gets them the same color. They've mixed the same 3 colors together.
It would be secure if someone who knew yellow
and could intercept these 2 transmissions
couldn't guess the secret colors.
This assumes that paint unmixing is impossible.
Probably not a very good assumption, and certainly not a practical way to actually do this.
What we want is to do the same thing with mathematics.
This is also the main operation we used in symmetric ciphers.
XOR = + mod 2.
To do key agreement, though, plus is not going to be the main operation.
The main operation we'll use is multiplication.
