Diffie and Hellman Key exchange

    

Scenario:

Create a shared encryption key for parties to communicate securely.

Solution:

A Symmetric encryption uses a single key shared between two entities while asymmetric encryption uses a pair of public key and a private key to encrypt and decrypt messages when communicating.

Diffie and Hellman [D & H] - Uses an asymmetric algorithm used to establish a shared secret for a symmetric key algorithm.

Entropy  - We need to make the key as big a possible, so it is near impossible to break, like 2048 bits or 4096 bits. 


Example: If Ram and Shyam wants to share messages securely they would use below algorithm by D & H to encrypt the keys
  1. Generate Keys

  2. --It uses 2 numbers. 
    
    -g: which is general number, which typically would be small
    
    -n: Large number, either 2048 or 4096 bits. 
    
    Each entity needs to have a secret. So for 2 parties, they would have secret each.
    
    g^secret MOD(n)
    
    Ex: Ram and Shyam in our example has to choose g & n. Lets say they agree upon:
    
    g = 5
    n = 11
    
    Then they select secret for themselves. Ram picks x = 2 and Shyam x = 4.
    
    Now;
    
    Ram's public key = 5^2 MOD(11) = 3
    
    Shyam's public key = 5^4 MOD(11) = 9
    
    Time for key exchange
    
    Ram => Shyam = Key: 3
    
    Shyam => ram = Key: 9
    
    As MODis one way function. So for example if you know that a square function 
    has output 9, then you can figure out the input would be 3. But if any one 
    gets hold of Shyam's key and they know its result of MOD function, 
    but they can figure out its the result of 5^4 MOD(11) as 9 could be MOD of 13MOD4 
    or 21MOD12 and so on. Here offcourse due to small numbers hacker can run a 
    loop to brute force figure i tout but with 4096 bits number it would 
    take may be millions of years ;)
    
    Now both side use one another public key and their secret (private key)
     to come up with shared key. So:
    
    Ram => 9 (Shyam's public key)^2 MOD 11 = 4
    
    Shyam => 3 (Ram's public key)^4 = 4
    
    Both come to same shared key that is 4. Now each one of them will use 4 to encrypt a message to send to one another.

  3. JavaScript
  4. const g = 5;
    const n = 11;
    
    const ramSecret = 2;
    const shyamSecret = 4;
    
    const publickey = (secret)=> Math.pow(g,secret) % n;
    
    const ramPublicKey = publickey(ramSecret);
    const shyamPublicKey = publickey(shyamSecret);
    
    console.log("Ram's Key",ramPublicKey);
    console.log("Shyam's Key",shyamPublicKey);
    
    const sharedKey = (publickey,secretKey) => {
        return Math.pow(publickey,secretKey) % n
    }
    
    const ramSharedKey = sharedKey(shyamPublicKey,ramSecret);
    const shyamSharedKey = sharedKey(ramPublicKey,shyamSecret);
    
    console.log("Ram's Shared Key",ramSharedKey);
    console.log("Shyam's Shared Key",shyamSharedKey);

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...