0% found this document useful (0 votes)
8 views2 pages

Q2) Implement The Diffie-Hellman Key Exchange Algorithm For Securely Exchanging Cryptographic Keys Between 2 Parties

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

Q2) Implement The Diffie-Hellman Key Exchange Algorithm For Securely Exchanging Cryptographic Keys Between 2 Parties

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

Q2] Implement the Diffie-Hellman key exchange algorithm for

securely exchanging cryptographic keys between 2 parties.


The Diffie-Hellman key exchange algorithm allows two parties, often called Party A and
Party B, to securely generate a shared secret over an insecure communication channel.
This shared secret can then be used as a key for symmetric encryption, enabling secure
communication.

Code for Diffie-Hellman key exchange algorithm:

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives.asymmetric import dh

# Step 1: Generate DH parameters

parameters = dh.generate_parameters(generator=2, key_size=2048,


backend=default_backend())

# Step 2: Generate private keys for both parties (A and B)

private_key_A = parameters.generate_private_key() private_key_B


=parameters.generate_private_key()

# Step 3: Generate public keys for both parties

public_key_A = private_key_A.public_key() public_key_B = private_key_B.public_key()

# Step 4: Exchange public keys and derive shared secret

shared_secret_A = private_key_A.exchange(public_key_B) shared_secret_B =


private_key_B.exchange(public_key_A)

# Step 5: Verify that both shared secrets are the same

assert shared_secret_A == shared_secret_B

# Step 6: Convert shared secret to a readable hexadecimal format

hex_shared_secret = shared_secret_A.hex()
Output:

Shared Secret (Hex): 0d7635bdba722396b0cc7728201535d0

Key Components:
default_backend() is used to select the default cryptographic backend (a standard
cryptography engine).

dh stands for Diffie-Hellman, and it contains the tools to create keys and parameters for
key exchange.

Generator (g): We set this to 2, which is a standard value used in many Diffie-Hellman
implementations. This value is a small base number used in modular arithmetic.

Key Size (p): The size of the prime number used in the Diffie-Hellman algorithm. We use
2048 bits, which is considered secure.

Private Keys: We generate the private keys for Party A and Party B. These keys are
randomly generated large numbers.

Public Keys: From the private keys, each party calculates a public key. The public key is
derived from the private key and is safe to share with others.

Party A uses its own private key (private_key_A) and Party B’s public key (public_key_B)
to compute shared_secret_A.

Party B uses its own private key (private_key_B) and Party A’s public key (public_key_A)
to compute shared_secret_B.

The assert statement checks that both parties have derived the same shared secret. If
shared_secret_A is not equal to shared_secret_B, an error will be raised.

The shared secret (a long sequence of bytes) is converted to a hexadecimal string.


Hexadecimal is a common way to represent binary data in a readable format, with each
byte represented by two hex characters.

We print the first 32 characters of the hexadecimal representation of the shared secret
for simplicity and readability.

You might also like