×

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

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 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 use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

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.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

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.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.