0% found this document useful (0 votes)
55 views4 pages

Worksheet 2.1

The document describes an experiment performing the Diffie-Hellman key exchange algorithm. It includes the aim, algorithm flowchart, C++ code implementation, and sample output. The Diffie-Hellman algorithm allows two parties to establish a shared secret over an insecure channel. It works by having each party generate a public/private key pair, and calculating a shared secret from the public keys. The code demonstrates generating keys for two parties and verifying they calculate the same shared secret.

Uploaded by

Jonny Ash
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)
55 views4 pages

Worksheet 2.1

The document describes an experiment performing the Diffie-Hellman key exchange algorithm. It includes the aim, algorithm flowchart, C++ code implementation, and sample output. The Diffie-Hellman algorithm allows two parties to establish a shared secret over an insecure channel. It works by having each party generate a public/private key pair, and calculating a shared secret from the public keys. The code demonstrates generating keys for two parties and verifying they calculate the same shared secret.

Uploaded by

Jonny Ash
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

Experiment No.

: 5

Student Name: Sahil Mallick UID: 19BCS1073


Branch: BE-CSE Section/Group: NTPP-IS 1A
Semester: 6th Date of Performance: 07/03/2022
Subject Name: Information Security Lab Subject Code: CSB-372

1. Aim: Perform the practical of Diffie Hellman Key Exchange Algorithm.

2. Algorithm/Flowchart:

The Diffie–Hellman (DH) Algorithm is a key-exchange protocol that enables two parties
communicating over public channel to establish a mutual secret without it being transmitted
over the Internet. DH enables the two to use a public key to encrypt and decrypt their
conversation or data using symmetric cryptography.

Diffie Hellman Key Exchange Algorithm for Key Generation:-


The algorithm is based on Elliptic Curve Cryptography, a method of doing public-key
cryptography based on the algebra structure of elliptic curves over finite fields. The DH also
uses the trapdoor function, just like many other ways to do public-key cryptography. The simple
idea of understanding to the DH Algorithm is the following.

1. The first party picks two prime numbers, g and p and tells them to the second party.
2. The second party then picks a secret number (let’s call it a), and then it computes ga mod p
and sends the result back to the first party; let’s call the result A. Keep in mind that the secret
number is not sent to anyone, only the result is.
3. Then the first party does the same; it selects a secret number b and calculates the result B
similor to the step 2.
4. Then, this result is sent to the second party.
5. The second party takes the received number B and calculates B(a) mod p.
6. The first party takes the received number A and calculates A(b) mod p.
The answer in step 5 will be the same as the answer in step 4. This means both parties will get
the same answer no matter the order of exponentiation.

3. Code:

#include<iostream>
using namespace std;

class DiffieHellman
{
public:
long long int p,g,x,a,y,b,A,B;
DiffieHellman(long long int p1,long long int g1,long long int
x1,long long int y1)
{
p = p1;
g = g1;
x = x1;
y = y1;
a=power(g,x,p);
b=power(g,y,p);
A = power(b,x,p);
B = power(a,y,p);
}

long long int power(int a,int b,int mod)


{
long long int t;
if(b==1)
return a;
t=power(a,b/2,mod);
if(b%2==0)
return (t*t)%mod;
else
return (((t*t)%mod)*a)%mod;
}
};

int main()
{
long long int p,g,x,a,y,b,A,B;
cout<<"Enter the values of p and g upon which both parties will aggree :
"<<endl;
cin>>p>>g;

cout<<"Enter the Secret Integer for 1st Party : ";


cin>>x;
cout<<"Enter the Secret Integer for 2nd Party : ";
cin>>y;
cout<<endl;
DiffieHellman dh(p,g,x,y);

cout<<"1st Party's private key, known only to 1st Party : "<<dh.a<<endl;


cout<<"2nd Party's private key known only to 2nd Party : "<<dh.b<<endl;
cout<<endl;
cout<<"1st Party's public key, known to both parties : "<<dh.A<<endl;
cout<<"2nd Party's public key, known to both parties : "<<dh.B<<endl;
return 0;
}

Code Snippet:
4. Output:

Learning outcomes:

1. Learnt about public-key cryptography.

2. Learnt about Diffie Hellman Key Exchange Algorithm.

3. Learnt the uses and advantages of Diffie Hellman Key Exchange Algorithm.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy