For symmetric key cryptography (same key for encryption and decryption), there must exist a way to securely create and agree on these secret keys.  You can't simply email someone your private key in plain text, otherwise an eavesdropper can obtain a copy and use it to decrypt your sensitive communication.  Imagine you are trying to have an online conversation with a friend, and some annoying turd is in the chat room reading everything you say.  You want to start an encryption channel (if you could) with your friend, so Mr. Turd can't understand what you're saying.  Since he is already listening, this makes agreeing on a secret encryption phrase very difficult.  In reality, a symmetric encryption key is used to obfuscate your data in a predictable and repeatable way, so that the person who received the encrypted data (ciphertext) can run the encrypted data through the decryption algorithm with the same key, and obtain the plain text message.  One of the oldest digital ways of being able to establish a shared secure key over an unsecured channel is the Diffie-Hellman key exchange (DHKE).  A lot of people use colors when explaining this, because it's easier than trying to explain modular arithmetic and primitive roots, but let's see where this takes us.  In the color analog, colors are used instead of large prime numbers which are used in the computing world.  I have made a picture, but I had to use photoshop instead of MS pain because I needed opacity and blending options not available in MS pain, but don't worry: I tried to keep the drawing as shitty as possible.  

In most encryption scenarios, the fictitious characters normally used are Alice and Bob.  This is boring.  I am instead going to use Beavis and Butthead.  So, Beavis and Butthead want to agree on a common shared secret that will be used to encrypt and decrypt their future communications.  In the color analog, they first publicly agree on a common shared color - red, in this case.  Turd the Eavesdropper intercepts this, and he now knows that their shared secret color is red.  Independently, Beavis and Butthead choose individual private colors that they do not send to each other.  In this case, those colors are Apple Schnapps Green and Tears of your Enemies Blue, respectively.  Beavis and Butthead then blend their secret colors with the red common color, and exchange this with each other.  Now, Turd has obtained the pretty color blends, as well as the red color previously obtained in the first step.  Now that Beavis and Butthead have the color blend of their shared common color and the other person's private color, they mix their own private color to the mix, which gives them both a common private color that has never been transmitted across the wire.  At this point, they are all set to go. In reality, they have a very large common secret prime number to use for encryption that was never sent across the wire.  The only information that Turd has is the common shared color of red, and the color blends which are, for all intents and purposes, inseparable.  Without the private colors, it will be practically impossible to derive the common private color.  




Now that you understand the general concept, how does this work with actual numbers?  We obviously don't use pretty color blends that are sent across the wire, but instead we use numbers.  Very large prime numbers.  I won't be using very large numbers because I hate math and anything larger than 100 confuses me.  So, let's start with some concepts.  The first of which is called modular arithmetic (also called clock arithmetic).  Imagine you have a circle (like a clock) numbered from 0-3.  This circle is called the modulus.  Now, also imagine you have a string of the same units, measuring 5 units.  The string is called the generator.  This is written as 5 mod 4  (generator followed by the modulus).  To visualize, you place the string at the top of the circle (0) and wind the string clockwise around the circle, and whatever number the string ends up on when wound around the circle is the answer.  This is not as confusing as my dumb ass made it out to be, so let me draw a diagram.......


Here you can see that a string of 5 units across a circle of 4 units ends on 1.  Also note, that the list of possible outcomes is always the modulus minus 1, since you start at 0.  This is also very similar to remainders when performing division.  Another way to look at this would be 5 divided by 4 has a remainder of 1.  Taking this a step further, you can also raise the generator to a power of x, as in 5mod 4 = 1.  In the DHKE, you will want to select a modulus that is a very large prime number, but in my scenario I will use 5.  Now, you will want to find a primitive root of 5, which is 3.  WTF is a primitive root?  A primitive root means that when raised to an exponent, there is an equal chance of the result being any value on the circle.  Let me try my hand at making a table to show how this works.  I'll use 3n mod 5 for this example


n 3n 3n mod 5
1 3 3
2 9 4
3 27 2
4 81 1

 In the above example, you can see that 3n mod 5 does not yield any repeats - the results are evenly distributed across the set of results seemingly at random.  Conversely, we can use the same technique against something that is not a primitive root of 5.  Say, 4.  Let's see the same expression done with 4n mod 5


n 4n 4n mod 5
1 4 4
2 16 1
3 64 4
4 256 1

Here, we can see that when raised to an exponent, the results are not uniformly distributed across the possible outcomes (ie, there are repeats). The use of a primitive root with its lack of repeated values is very important because it creates a one way function - something that is easy to compute in one direction, but hard to reverse.  For instance, if you were given the following: 3n mod 5 = 2, and had to figure out the exponent that 3 was raised to in order to get 2, you would have to basically perform trial and error, since each exponent is equally likely to be anywhere between 0-4.  This is called the discrete logarithm problem, and is an important property in encryption.  So now that we have a prime modulus and a primitive root, where do we go from here in terms of agreeing on a shared secret for communications?  

First, Beavis and Butthead publicly agree on a prime modulus and generator (this is equivalent to the color red used in the coloring example above).  For this, we will use 5n mod 23 (5 is a primitive root of 23 because I googled it).  So, this acts as their red color that they send in the clear, and now Turd has this information.  Next, Beavis chooses a secret number to raise 5 to the power of, and sends the result to Butthead.  Beavis chooses 10, and sends the results of 510 mod 23 to Butthead, which is 9.  Butthead also chooses a secret number, and performs the same operation, and sends the result to Beavis.  Butthead chooses 15, and sends the result to Beavis, which is 19, since 515 mod 23 = 19.  In the coloring example, 9 and 19 act as the color blends that are send to each other.  Here is where the magic happens.  Butthead takes the public result that he received from Beavis, and raises it to the power of his own secret number in the same formula.  So, For Butthead it would be 915 mod 23, which equals 6.  Beavis performs the same thing with Butthead's public result and his own private number, which is 1910 mod 23, which equals 6.  Notice now, that they both have independently got to the same shared secret, 6, without ever sending it over the wire.  I'll make another picture so it's easier to follow.  


With only the common modulus, generator, and the results of the secret operation, Turd would have to resort to trial and error to come up with the list of possible secret numbers.  This isn't difficult to do in the above sample, but in reality, the prime number is massively large, and resorting to trial and error becomes impractical due to the amount of computing time it would take (thousands of years).  And there you have it!