Ex 6,7
Ex 6,7
• 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;
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 ("Eve") 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;
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.