×

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

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

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.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

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.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.