Assignment No 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Assignment no 2 (Marks 5)

1) Explain Diffie-Hellman with the help of example (not mentioned in slides).


2) Explain RSA algorithm with the help of example (not mentioned in slides).

Answer Q1:

1. In Public key encryption schemes are secure only if authenticity of the public key is
assured.
2. Diffie-Hellman key exchange is a simple public key algorithm.
3. The protocol enables 2 users to establish a secret key using a public key scheme based
on discrete algorithms.
4. The protocol is secure only if the authenticity of the 2 participants can be established.
5. or this scheme, there are 2 publicly known numbers :
o A prime number q
o An integer α that is a primitive root of q.

(Note: Premitive root of a prime number P is one, whose powers module P generate all
the images from 1 to P-1)
6. Suppose users A and B wish to exchange the key.
User A selects a random integer XA<qXA<q and computes
YA=αXAmod qYA=αXAmod q
7. User B independently selects a random integer XB<qXB<q and compute
YB=αXBmod qYB=αXBmod q
8. Each side keeps X value private and makes Y value available publicly to the other side
user A computes the key as:
k=(YB)XAmod qk=(YB)XAmod q
User B computes the key as :
k=(YA)XBmod qk=(YA)XBmod q
The calculations produce identical results :
k=(YB)XAmod q−>calculated by user
A=(αXBmod q)XAmod q=(αXB)XA(mod q)−>By rules of modular
arithmetic=αXB XAmod q=(αXA)XBmod qk=(YB)XAmod q−>calculated by user
A=(αXBmod q)XAmod q=(αXB)XA(mod q)−>By rules of modular
arithmetic=αXB XAmod q=(αXA)XBmod q
k=(αXAmod q)XBmod qk=(αXAmod q)XBmod q
9. Diffie Hellman key Exchange Algorithm
1. k=(YA)XBmodqk=(YA)XBmodq -> same as calculated by B
2. Global Public Elements
q ; prime number
α ; α < q and it is primitive root of q
3. USER A KEY GENERATION
Select Private key XAXA<qXAXA<q
Calculation of Public key YAYA=αXAmod qYAYA=αXAmod q
4. USER B KEY GENERATION

Select Private key XBXB<qXBXB<q


Calculation of Public key YBYB=αXBmod qYBYB=αXBmod q
5. Calculation of Secret Key by A

k=(YB)XAmod qk=(YB)XAmod q
6. Calculation of Secret Key by B
k=(YA)XBmod qk=(YA)XBmod q
10. The result is that two sides have exchanged a secret value.
11. Since XAXA and XBXB are private the other party can work only following ingredients:
q,α,XA,XBq,α,XA,XB
Note: YB=αXBYB=αXB mod a
XB=dlogα,q(YB)XB=dlog⁡α,q(YB)
↑↑
 Discrete Logarithm
12. The algorithm security lies on the fact that it is easy to calculate exponential
modulo a prime, last difficult to calculate to calculate discrete logarithm.

Figure 5.6 Diffie-Hellman Exchange AlgorithmFigure 5.6 Diffie-Hellman Exchange


Algorithm

Example:
Consider q=353, α= 3 ( 3 is primitive root of 353)
A and B discrete private keys
X/A=97andXB=223X/A=97andXB=223
Each computes its public key
A computes YA=397YA=397 mod 353 =40
B computes YB=3233YB=3233 mod 353 = 248
After exchange of public keys, each can compute the common secret key
A computes
K =(YB)XAmod 353=(248)97mod 353=160=(YB)XAmod 353=(248)97mod 353=160
B computes K =(YA)XBmod 353=(40)253mod 353=160

Answer Q2:

RSA Algorithm in Cryptography

RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works on two
different keys i.e. Public Key and Private Key. As the name describes that the Public Key is given to
everyone and Private key is kept private.

An example of asymmetric cryptography :

1. A client (for example browser) sends its public key to the server and requests for some data.

2. The server encrypts the data using client’s public key and sends the encrypted data.

3. Client receives this data and decrypts it.

Since this is asymmetric, nobody else except browser can decrypt the data even if a third party has
public key of browser.

The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer. The public
key consists of two numbers where one number is multiplication of two large prime numbers. And
private key is also derived from the same two prime numbers. 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. RSA keys can be
typically 1024 or 2048 bits long, but experts believe that 1024 bit keys could be broken in the near
future. But till now it seems to be an infeasible task.

Let us learn the mechanism behind RSA algorithm :

>> Generating Public Key :


 Select two prime no's. Suppose P = 53 and Q = 59.

 Now First part of the Public key : n = P*Q = 3127.

 We also need a small exponent say e : But e Must be

o An integer.

o Not be a factor of n.

o 1 < e < Φ(n) [Φ(n) is discussed below], Let us now consider it to be equal to 3.

 Our Public Key is made of n and e

>> Generating Private Key :

 We need to calculate Φ(n) :

 Such that Φ(n) = (P-1)(Q-1) so, Φ(n) = 3016

 Now calculate Private Key, d : d = (k*Φ(n) + 1) / e for some integer k

 For k = 2, value of d is 2011.

Now we are ready with our – Public Key ( n = 3127 and e = 3) and Private Key(d = 2011)

Now we will encrypt “HI” :

 Convert letters to numbers : H = 8 and I = 9

 Thus Encrypted Data c = 89e mod n. Thus our Encrypted Data comes out to be 1394 Now we
will decrypt 1394 :

 Decrypted Data = cd mod n. Thus our Encrypted Data comes out to be 89

 8 = H and I = 9 i.e. "HI".

Below is C implementation of RSA algorithm for small values:

#include<stdio.h>
#include<math.h>

int gcd(int a, int h)

    int temp;

    while (1)

    {

        temp = a%h;

        if (temp == 0)

          return h;

        a = h;

        h = temp;

    }

int main()

    double p = 3;

    double q = 7;

    double n = p*q;

    double e = 2;

    double phi = (p-1)*(q-1);

    while (e < phi)

    {

        if (gcd(e, phi)==1)

            break;

        else
            e++;

    }

    int k = 2;

    double d = (1 + (k*phi))/e;

    double msg = 20;

    printf("Message data = %lf", msg);

    double c = pow(msg, e);

    c = fmod(c, n);

    printf("\nEncrypted data = %lf", c);

    double m = pow(c, d);

    m = fmod(m, n);

    printf("\nOriginal Message Sent = %lf", m);

    return 0;

Output :

Message data = 12.000000

Encrypted data = 3.000000

Original Message Sent = 12.000000

You might also like