0% found this document useful (0 votes)
25 views8 pages

Ex 6,7

The document describes implementing RSA encryption and decryption. It explains generating a key pair by choosing prime numbers p and q, calculating the modulus n and Euler's totient function phi(n), and exponents e and d. Encryption works by raising a message m to the public exponent e modulo n. Decryption recovers m by raising the ciphertext to the private exponent d modulo n.
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)
25 views8 pages

Ex 6,7

The document describes implementing RSA encryption and decryption. It explains generating a key pair by choosing prime numbers p and q, calculating the modulus n and Euler's totient function phi(n), and exponents e and d. Encryption works by raising a message m to the public exponent e modulo n. Decryption recovers m by raising the ciphertext to the private exponent d modulo n.
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/ 8

Experiment 6

AIM: To implement a program to show key using Diffie-Hellman key exchange.


Introduction and Theory:
In the realm of secure communication and data encryption, the Diffie-Hellman key exchange algorithm
stands as a cornerstone in ensuring confidentiality and privacy. Developed by Whitfield Diffie and Martin
Hellman in 1976, this cryptographic method revolutionized secure communication by enabling two
parties to establish a shared secret key over an insecure channel, without the need for a pre-existing
shared key or secure communication prior to the key exchange. This key exchange process has since
become a fundamental component of various encryption protocols, such as SSL/TLS for secure web
browsing, SSH for secure remote access, and many more.
This implementation guide aims to elucidate the theory and practical application of the Diffie-Hellman
key exchange algorithm. By understanding the underlying principles and performing a step-by-step
implementation, you will gain a deeper insight into how this algorithm works and how it can be applied to
ensure secure communication in a digital world.
The Diffie-Hellman key exchange is based on the mathematical properties of modular exponentiation and
the difficulty of solving the discrete logarithm problem. The key idea behind this algorithm is that two
parties, Alice and Bob, can agree upon a shared secret key, even if an eavesdropper, often referred to as
Eve, listens in on their communication. The security of this algorithm relies on the computational
complexity of solving the discrete logarithm problem, which is considered a hard problem for sufficiently
large numbers.
Here's a high-level overview of how the Diffie-Hellman key exchange works:
1. Setup:
- Choose a large prime number 'p' and a primitive root 'g' modulo 'p'. These values are public and can be
shared openly.
- Both Alice and Bob independently select their secret keys, 'a' and 'b', which are kept private.
2. Key Generation:
- Alice computes 'A = (g^a) % p' and sends 'A' to Bob.
- Bob computes 'B = (g^b) % p' and sends 'B' to Alice.
3. Key Exchange:
- Alice receives 'B' and calculates the shared secret key 's' as 's = (B^a) % p'.
- Bob receives 'A' and calculates the same shared secret key 's' as 's = (A^b) % p'.
4. Shared Secret:
- Both Alice and Bob now have the same shared secret key 's', which can be used for secure
communication using symmetric-key encryption.
Algorithm:
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 Ci (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 received the key and from
that generates a secret key after which they have the same secret key to encrypt.

Code :

#include <cmath>
#include <iostream>
using namespace std;
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);
}
int main()
{
long long int P, G, x, a, y, b, ka, kb;
P = 47; // A prime number P is taken
cout << "The value of P : " << P << endl;

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


cout << "The value of G : " << G << endl;
a = 8; // a is the chosen private key
cout << "The private key a for Alice : " << a << endl;
x = power(G, a, P); // gets the generated key
b = 4; // b is the chosen private key
cout << "The private key b for Bob : " << b << endl;
y = power(G, b, P); // gets the generated key
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 :

Discussion:
❖ In Diffie Hellman key exchange algorithm, the order of the group G must be large, 2. particularly
if the same group is used for large amounts of traffic.
❖ The eavesdropper (&quot;Eve&quot;) has to solve the Diffie-Hellman problem to obtain gab.
This is currently considered difficult for groups whose order is large enough. efficient algorithm
to solve the discrete logarithm problem would make it easy to
❖ An compute a or b and solve the Diffie-Hellman problem, making this and many other public key
cryptosystems insecure. Fields of small characteristic may be less secure.
Findings and Learnings:
❖ Diffie-Hellman key exchange is a cryptographic protocol that allows two parties that have no
prior knowledge of each other to jointly establish a shared secret key over an insecure
communications channel.
❖ This key can then be used to encrypt subsequent communications using a symmetric key cipher.
Experiment 7
Aim: to implement a program to show encryption and decryption in rsa algorithm
Introduction and theory:
In today's interconnected and digital world, information security is of paramount importance. The RSA
(Rivest-Shamir-Adleman) algorithm is a cornerstone of modern cryptography, widely used for secure data
transmission and digital signatures. Named after its inventors, Ron Rivest, Adi Shamir, and Leonard
Adleman, RSA is a public-key encryption system that provides a robust means of encrypting sensitive
information and ensuring the confidentiality and authenticity of digital communications.
This implementation guide aims to provide a comprehensive overview of the RSA encryption and
decryption process. By delving into the theoretical underpinnings and performing a step-by-step
implementation, you will gain a deeper understanding of how the RSA algorithm works and how it can be
applied to secure data exchange in a digital environment.
The RSA algorithm is a public-key cryptosystem, which means it uses a pair of keys for encryption and
decryption: a public key for encryption and a private key for decryption. The security of RSA is based on
the difficulty of factoring large semiprime numbers.
Here's a high-level overview of how the RSA encryption and decryption process works:
**Key Generation:**
❖ Select two large prime numbers, 'p' and 'q'. These prime numbers are kept secret.
❖ Calculate the modulus 'n' as 'n = p * q'. The modulus is part of both the public and private keys.
❖ Compute the Euler's totient function 'φ(n)' as 'φ(n) = (p-1) * (q-1)'.
❖ Choose a public exponent 'e' such that '1 < e < φ(n)' and 'e' is coprime with 'φ(n)'. Typically, 'e' is
a small prime, such as 65537 (2^16 + 1).
❖ Calculate the private exponent 'd' as the modular multiplicative inverse of 'e' modulo 'φ(n)',
denoted as 'd ≡ e^(-1) (mod φ(n))'. The private key includes 'd'.
**Encryption:**
To encrypt a message 'M' using the recipient's public key ('n', 'e'), perform the following steps:
➢ Convert the message 'M' to an integer 'm', where '0 <= m < n'.
➢ Compute the ciphertext 'C' as 'C = m^e (mod n)'.
➢ Send 'C' to the recipient.
**Decryption:**
To decrypt the ciphertext 'C' using the recipient's private key ('n', 'd'), do the following:
✓ Compute the plaintext 'm' as 'm = C^d (mod n)'.
✓ Convert 'm' back to the original message 'M'.

The security of RSA relies on the difficulty of factoring the modulus 'n' into its prime factors ('p' and 'q').
As long as 'p' and 'q' are chosen to be large enough, RSA encryption is considered secure.
Algorithm :
Generate an RSA key pair.
INPUT: Required modulus bit length, kk.
OUTPUT: An RSA key pair ((N,e),d) ((N,e),d) where N is the modulus, the product of two primes
(N=pqN=pq) not exceeding kk bits in length; ee is the public exponent, a number less than and coprime to
(p-1)(q-1)(p-1)(q-1); and dd is the private exponent such that ed=1mod(p-1)(q-1)ed=1mod(p-1)(q-1).
❖ Select a value of ee from 3,5,17,257,655373,5,17,257,65537
❖ repeat
❖ p genprime(k/2)
❖ until (pmode)#1 (pmode)#1
❖ repeat
❖ qgenprime(k - k/2)
❖ until (qmode)#1(qmode)#1
❖ N-pq
❖ L (p-1)(q-1)
❖ dmodinv(e, L)
❖ return (N,e,d)(N,e,d)
Encryption
Sender A does the following:-
❖ Obtains the recipient B's public key (n,e)(n,e).
❖ Represents the plaintext message as a positive integer mm with 1<m<nl<m<n.
❖ Computes the ciphertext c-memodne memodn.
❖ Sends the ciphertext cc to B.
Decryption
Recipient B does the following:-
1. Uses his private key (n,d)(n,d) to compute m-cdmodnm=cdmodn.
2. Extracts the plaintext from the message representative mm.

Code :

#include <iostream>
#include <cmath>
using namespace std;

// calculate phi(n) for a given number n


int phi(int n) {
int result = n;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
result -= result / i;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}

// calculate gcd(a, b) using the Euclidean algorithm


int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}

// calculate a^b mod m using modular exponentiation


int modpow(int a, int b, int m) {
int result = 1;
while (b > 0) {
if (b & 1) {
result = (result * a) % m;
}
a = (a * a) % m;
b >>= 1;
}
return result;
}

// generate a random primitive root modulo n


int generatePrimitiveRoot(int n) {
int phiN = phi(n);
int factors[phiN], numFactors = 0;
int temp = phiN;
// get all prime factors of phi(n)
for (int i = 2; i <= sqrt(temp); i++) {
if (temp % i == 0) {
factors[numFactors++] = i;
while (temp % i == 0) {
temp /= i;
}
}
}
if (temp > 1) {
factors[numFactors++] = temp;
}
// test possible primitive roots
for (int i = 2; i <= n; i++) {
bool isRoot = true;
for (int j = 0; j < numFactors; j++) {
if (modpow(i, phiN / factors[j], n) == 1) {
isRoot = false;
break;
}
}
if (isRoot) {
return i;
}
}
return -1;
}

int main() {
int p = 61;
int q = 53;
int n = p * q;
int phiN = (p - 1) * (q - 1);
int e = generatePrimitiveRoot(phiN);
int d = 0;
while ((d * e) % phiN != 1) {
d++;
}
cout << "Public key: {" << e << ", " << n << "}" << endl;
cout << "Private key: {" << d << ", " << n << "}" << endl;
int m = 123456;
int c = modpow(m, e, n);
int decrypted = modpow(c, d, n);
cout << "Original message: " << m << endl;
cout << "Encrypted message: " << c << endl;
cout << "Decrypted message: " << decrypted << endl;
return 0;
}
Output :

Discussion:
▪ In RSA, both the keys (public and private) can encrypt a message; the opposite key from the 2
one used to encrypt a message is used to decrypt it.
▪ . This is one of the major reasons why RSA has become the most widely used asymmetric
algorithm: It provides a method to assure the confidentiality, integrity, authenticity, and non-
repudiation of electronic communications and data storage.
▪ So if somebody can factorize the large number, the private key is compromised. Therefore
encryption strength totally lies on the key size and if we double or triple the key size, the strength
of encryption increases exponentially.
Findings and Learnings:
❖ It is a public key cipher, that is, a public key is used to encrypt a message M.
❖ The encrypted message of M can only be decrypted using the private key that is paired with the
public key that was used to encrypt the message.
❖ It is a block code, i.e., plaintext is encrypted in blocks.

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