×

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

AliceBob
Public Keys Obtainable = P, GPublic Keys Obtainable = P, G  
Private key picked equals aPrivate key chosen = b  
produced key =x=Ga mod P.  produced key =Gb mod P = y  
Keys generated are traded  Keys generated are traded  
y = obtain key,x = get key  
private key produced = y a mod P = k a  private key produced = k b = P mod xb  

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.

Related Topics

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

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...

4 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.