# Diffie-Hellman Algorithm in C

Background:

• A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields.
• To ensure equal security, ECC encryption requires fewer keys than non-ECC encryption (256-bit ECC security has the same security as 3072-bit RSA encryption).
• Understanding the fundamentals of elliptic curves is crucial for understanding elliptic curve cryptography.
• An equation of the type describes an elliptic curve, a planar algebraic curve.
y^2 = x^3 + axe + b
• where "a" denotes the x-coefficient and "b" denotes an expression constant.
• It is not singular to the curve.
• Thus, there are no peaks or self-intersections on the graph (if the coefficient array characteristic equals 2 or 3)
• An elliptic curve typically resembles the image below.
• When a straight line crosses the curve, an elliptic curve can nearly always intersect three locations.
• The elliptic curve is symmetric about the x-axis, as you can see.
• This characteristic is crucial to the elliptic curve method.

### Algorithm Diffie-Hellman

• The Diffie-Hellman technique creates a shared secret that may be utilised for confidential communication while exchanging data over public networks by using an elliptic curve to produce points and a private key to retrieve parameters.
• we take only four variables into account for the algorithm's practical implementation and simplicity: prime figures P and G, as well as the two private values a and b, are the fundamental roots of P.
• P and G are both open numbers. Users (like Alice and Bob) select private values a and b, create keys, and then publicly exchange them. The key is obtained by another party, who then creates a private key and encrypts it.

### Step-by-step Guidelines

This is algebraically demonstrable.

k a = k b

In order to encrypt, the user now possesses a symmetric private key.

``````#include <stdio.h>
// Function to compute `i^t mod n`
int compute(int i, int t, int n)  {
int q,z = 1;
while (t> 0)  {
q= m % 2;
if (r == 1) {
z= (z*i) % n;  }
a = i*i% n;
t= t / 2;}
return y;}
// demonstrating the Diffie-Hellman algorithm in c language
int main()  {
int f = 23;        // modulus
int h = 5;        // base
int i, j;    // `i` – Alice's secret key, `j` – Bob's secret key.
int I, J;    // `I` – Alice's public key, `J` – Bob's public key
// choose a secret integer for Alice's private key (only known to Alice)
i = 6;        // or, use `rand()`
// Calculate Alice's public key (Alice will send `I` to Bob)
I = compute(h, i, f);
// choose a secret integer for Bob's private key (only known to Bob)
j = 15;        // or, use `rand()`
// Calculate Bob's public key (Bob will send `J` to Alice)
J = compute(h, j, f);
// Alice and Bob Exchange their public key `I` and `J` with each other
// Find secret key
int keyI = compute(J, i, f);
int keyJ = compute(I, j, f);
printf("Alice's secret key is %d\nBob's secret key is %d", keyI, keyJ);
return 0;
}
``````

Output:

``````Alice’s secret key is 2
Bob’s secret key is 2
``````
• The actual algorithm is really basic. Here is an example protocol with the secret value indicated in red, for when Alice wishes to communicate a secret with Bob.
• In order to allow the resultant shared secret to have any value between 1 and p-1, Alice and Bob decide to use prime p = 23 and base g = 5. A = ga mod p (A = 56 mod 23 = 8) is the message Alice delivers to Bob using the secret number a = 6.
• Alice transmits B = gb mod p (B = 515 mod 23 = 19) after Bob selects a secret integer of size b = 15 for him.
• For Alice, s = Ba mod p equals 2 (s = 196 mod 23).
• Bob discovers the formula s = Ab mod p (s = 815 mod 23 = 2).
• Bob and Alice have a secret (number 2) The result Alice receives in step four is the same result Bob received in step five.
• Bob does the math
• Gave Ab mod p equals (ga mod p)b mod p and mod p.
• Alice does the math
• gba mod p = ba mod p = (gb mod p)a mod p
• To exchange encryption keys for use with symmetric encryption techniques like AES, the Diffie-Hellman algorithm is typically utilised. Note that no data is sent while keys are being exchanged.
• Here, the key is jointly created by the two parties.