0% found this document useful (0 votes)
10 views

CSS exp1_rollno__17

The document outlines an experiment on the Diffie-Hellman algorithm for establishing a shared secret key for secure communications over a public network. It includes a theoretical explanation, a code implementation in C++, and demonstrates how two users can generate and exchange keys to derive a common secret key. The conclusion emphasizes the algorithm's effectiveness in enabling encrypted communication without transmitting the secret key directly.

Uploaded by

krutikapandya23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CSS exp1_rollno__17

The document outlines an experiment on the Diffie-Hellman algorithm for establishing a shared secret key for secure communications over a public network. It includes a theoretical explanation, a code implementation in C++, and demonstrates how two users can generate and exchange keys to derive a common secret key. The conclusion emphasizes the algorithm's effectiveness in enabling encrypted communication without transmitting the secret key directly.

Uploaded by

krutikapandya23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Universal College of Engineering, Kaman

Department of Computer Engineering


Subject: Cryptography and System Security

Experiment No: 4
Roll No: 55 Name: Krutika pandya Div: B Batch: B1

Aim: Diffie Hellman algorithm


Theory: The Diffie-Hellman algorithm is being used to establish a shared secret that can be used
for secret communications while exchanging data over a public network using the elliptic curve
to generate points and get the secret key using the parameters.
● For the sake of simplicity and practical implementation of the algorithm, we will
consider only 4 variables, one prime P and G (a primitive root of P) and two private
values a and b.
● P and G are both publicly available numbers. Users (say Alice and Bob) pick private
values a and b and they generate a key and exchange it publicly. The opposite person
receives the key and that generates a secret key, after which they have the same secret
key to encrypt.

Code-

#include <cmath>

#include <iostream>

using namespace std;

// Power function to return value of a ^ b mod P

long long int power(long long int a, long long int b,

long long int P)

{ if (b == 1)
return a;

else

return (((long long int)pow(a, b)) % P);

// Driver program

int main()

long long int P, G, x, a, y, b, ka, kb;

// Both the persons will be agreed upon the

// public keys G and P

P = 23; // A prime number P is taken

cout << "The value of P : " << P << endl;

G = 9; // A primitive root for P, G is taken

cout << "The value of G : " << G << endl;

// Alice will choose the private key a

a = 4; // a is the chosen private key

cout << "The private key a for Alice : " << a << endl;

x = power(G, a, P); // gets the generated key

// Bob will choose the private key b

b = 3; // b is the chosen private key


cout << "The private key b for Bob : " << b << endl;

y = power(G, b, P); // gets the generated key

// Generating the secret key after the exchange

// of keys

ka = power(y, a, P); // Secret key for Alice

kb = power(x, b, P); // Secret key for Bob

cout << "Secret key for the Alice is : " << ka << endl;

cout << "Secret key for the Bob is : " << kb << endl;

return 0;

Output:-

Conclusion:- The Diffie-Hellman key exchange allows two parties to securely share a secret key
using public values and private keys. This ensures encrypted communication without directly
transmitting the secret key.

You might also like