Implementierung des Diffie-Hellman-Algorithmus

Diffie-Hellman-Algorithmus:

Der Diffie-Hellman-Algorithmus wird verwendet, um ein gemeinsames Geheimnis zu etablieren, das für geheime Kommunikationen beim Datenaustausch über ein öffentliches Netzwerk verwendet werden kann. Dabei wird die elliptische Kurve verwendet, um Punkte zu generieren und mithilfe der Parameter den geheimen Schlüssel zu erhalten.  

  • Der Einfachheit halber und der praktischen Umsetzung des Algorithmus halber betrachten wir nur vier Variablen, eine Primzahl P und G (eine Primitivwurzel von P) und zwei private Werte a und b.
  • P und G sind beide öffentlich verfügbare Nummern. Benutzer (z. B. Alice und Bob) wählen die privaten Werte a und b aus, generieren einen Schlüssel und tauschen ihn öffentlich aus. Die Gegenperson erhält den Schlüssel und dieser generiert einen geheimen Schlüssel, nach dem sie denselben geheimen Schlüssel verschlüsseln muss.


Die Schritt-für-Schritt-Erklärung lautet wie folgt:  

Alice Bob
Öffentliche Schlüssel verfügbar = P G Öffentliche Schlüssel verfügbar = P G
Ausgewählter privater Schlüssel = a Ausgewählter privater Schlüssel = b

Schlüssel generiert = 

x = G^a mod P

Schlüssel generiert = 

y = G^b mod P

Es findet ein Austausch der generierten Schlüssel statt
Schlüssel erhalten = y Schlüssel erhalten = x

Generierter geheimer Schlüssel = 

k_a = y^a mod P

Generierter geheimer Schlüssel = 

k_b = x^b mod P

Algebraisch lässt sich das zeigen 

k_a = k_b

Benutzer verfügen nun über einen symmetrischen geheimen Schlüssel zum Verschlüsseln

Beispiel:  

 Step 1: Alice and Bob get public numbers P = 23 G = 9   
Step 2: Alice selected a private key a = 4 and
Bob selected a private key b = 3
Step 3: Alice and Bob compute public values
Alice: x =(9^4 mod 23) = (6561 mod 23) = 6
Bob: y = (9^3 mod 23) = (729 mod 23) = 16
Step 4: Alice and Bob exchange public numbers
Step 5: Alice receives public key y =16 and
Bob receives public key x = 6
Step 6: Alice and Bob compute symmetric keys
Alice: ka = y^a mod p = 65536 mod 23 = 9
Bob: kb = x^b mod p = 216 mod 23 = 9
Step 7: 9 is the shared secret.

Durchführung:   

C++
   /* This program calculates the Key for two persons   using the Diffie-Hellman Key exchange algorithm using C++ */   #include         #include          using     namespace     std  ;   // Power function to return value of a ^ b mod P   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  );   }   // Driver program   int     main  ()   {      long     long     int     P       G       x       a       y       b       ka       kb  ;      // Both the persons will be agreed upon the      // public keys G and P      P     =     23  ;     // A prime number P is taken      cout      < <     'The value of P : '      < <     P      < <     endl  ;      G     =     9  ;     // A primitive root for P G is taken      cout      < <     'The value of G : '      < <     G      < <     endl  ;      // Alice will choose the private key a      a     =     4  ;     // a is the chosen private key      cout      < <     'The private key a for Alice : '      < <     a      < <     endl  ;      x     =     power  (  G       a       P  );     // gets the generated key      // Bob will choose the private key b      b     =     3  ;     // b is the chosen private key      cout      < <     'The private key b for Bob : '      < <     b      < <     endl  ;      y     =     power  (  G       b       P  );     // gets the generated key      // Generating the secret key after the exchange      // of keys      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  ;   }   // This code is contributed by Pranay Arora   
C
   /* This program calculates the Key for two persons   using the Diffie-Hellman Key exchange algorithm */   #include         #include         // Power function to return value of a ^ b mod P   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  );   }   // Driver program   int     main  ()   {      long     long     int     P       G       x       a       y       b       ka       kb  ;      // Both the persons will be agreed upon the      // public keys G and P      P     =     23  ;     // A prime number P is taken      printf  (  'The value of P : %lld  n  '       P  );      G     =     9  ;     // A primitive root for P G is taken      printf  (  'The value of G : %lld  nn  '       G  );      // Alice will choose the private key a      a     =     4  ;     // a is the chosen private key      printf  (  'The private key a for Alice : %lld  n  '       a  );      x     =     power  (  G       a       P  );     // gets the generated key      // Bob will choose the private key b      b     =     3  ;     // b is the chosen private key      printf  (  'The private key b for Bob : %lld  nn  '       b  );      y     =     power  (  G       b       P  );     // gets the generated key      // Generating the secret key after the exchange      // of keys      ka     =     power  (  y       a       P  );     // Secret key for Alice      kb     =     power  (  x       b       P  );     // Secret key for Bob      printf  (  'Secret key for the Alice is : %lld  n  '       ka  );      printf  (  'Secret Key for the Bob is : %lld  n  '       kb  );      return     0  ;   }   
Java
   // This program calculates the Key for two persons   // using the Diffie-Hellman Key exchange algorithm   class   GFG     {      // Power function to return value of a ^ b mod P      private     static     long     power  (  long     a       long     b       long     p  )      {      if     (  b     ==     1  )      return     a  ;      else      return     (((  long  )  Math  .  pow  (  a       b  ))     %     p  );      }      // Driver code      public     static     void     main  (  String  []     args  )      {      long     P       G       x       a       y       b       ka       kb  ;      // Both the persons will be agreed upon the      // public keys G and P      // A prime number P is taken      P     =     23  ;      System  .  out  .  println  (  'The value of P:'     +     P  );      // A primitive root for P G is taken      G     =     9  ;      System  .  out  .  println  (  'The value of G:'     +     G  );      // Alice will choose the private key a      // a is the chosen private key      a     =     4  ;      System  .  out  .  println  (  'The private key a for Alice:'      +     a  );      // Gets the generated key      x     =     power  (  G       a       P  );      // Bob will choose the private key b      // b is the chosen private key      b     =     3  ;      System  .  out  .  println  (  'The private key b for Bob:'      +     b  );      // Gets the generated key      y     =     power  (  G       b       P  );      // Generating the secret key after the exchange      // of keys      ka     =     power  (  y       a       P  );     // Secret key for Alice      kb     =     power  (  x       b       P  );     // Secret key for Bob      System  .  out  .  println  (  'Secret key for the Alice is:'      +     ka  );      System  .  out  .  println  (  'Secret key for the Bob is:'      +     kb  );      }   }   // This code is contributed by raghav14   
Python
   # Diffie-Hellman Code   # Power function to return value of a^b mod P   def   power  (  a     b     p  ):   if   b   ==   1  :   return   a   else  :   return   pow  (  a     b  )   %   p   # Main function   def   main  ():   # Both persons agree upon the public keys G and P   # A prime number P is taken   P   =   23   print  (  'The value of P:'     P  )   # A primitive root for P G is taken   G   =   9   print  (  'The value of G:'     G  )   # Alice chooses the private key a   # a is the chosen private key   a   =   4   print  (  'The private key a for Alice:'     a  )   # Gets the generated key   x   =   power  (  G     a     P  )   # Bob chooses the private key b   # b is the chosen private key   b   =   3   print  (  'The private key b for Bob:'     b  )   # Gets the generated key   y   =   power  (  G     b     P  )   # Generating the secret key after the exchange of keys   ka   =   power  (  y     a     P  )   # Secret key for Alice   kb   =   power  (  x     b     P  )   # Secret key for Bob   print  (  'Secret key for Alice is:'     ka  )   print  (  'Secret key for Bob is:'     kb  )   if   __name__   ==   '__main__'  :   main  ()   
C#
   // C# implementation to calculate the Key for two persons   // using the Diffie-Hellman Key exchange algorithm   using     System  ;   class     GFG     {      // Power function to return value of a ^ b mod P      private     static     long     power  (  long     a       long     b       long     P  )      {      if     (  b     ==     1  )      return     a  ;      else      return     (((  long  )  Math  .  Pow  (  a       b  ))     %     P  );      }      public     static     void     Main  ()      {      long     P       G       x       a       y       b       ka       kb  ;      // Both the persons will be agreed upon the      // public keys G and P      P     =     23  ;     // A prime number P is taken      Console  .  WriteLine  (  'The value of P:'     +     P  );      G     =     9  ;     // A primitive root for P G is taken      Console  .  WriteLine  (  'The value of G:'     +     G  );      // Alice will choose the private key a      a     =     4  ;     // a is the chosen private key      Console  .  WriteLine  (  'nThe private key a for Alice:'      +     a  );      x     =     power  (  G       a       P  );     // gets the generated key      // Bob will choose the private key b      b     =     3  ;     // b is the chosen private key      Console  .  WriteLine  (  'The private key b for Bob:'     +     b  );      y     =     power  (  G       b       P  );     // gets the generated key      // Generating the secret key after the exchange      // of keys      ka     =     power  (  y       a       P  );     // Secret key for Alice      kb     =     power  (  x       b       P  );     // Secret key for Bob      Console  .  WriteLine  (  'nSecret key for the Alice is:'      +     ka  );      Console  .  WriteLine  (  'Secret key for the Alice is:'      +     kb  );      }   }   // This code is contributed by Pranay Arora   
JavaScript
    <  script  >   // This program calculates the Key for two persons   // using the Diffie-Hellman Key exchange algorithm    // Power function to return value of a ^ b mod P   function     power  (  a       b       p  )      {      if     (  b     ==     1  )      return     a  ;      else      return  ((  Math  .  pow  (  a       b  ))     %     p  );   }   // Driver code   var     P       G       x       a       y       b       ka       kb  ;   // Both the persons will be agreed upon the   // public keys G and P   // A prime number P is taken   P     =     23  ;   document  .  write  (  'The value of P:'     +     P     +     '  
'
); // A primitive root for P G is taken G = 9 ; document . write ( 'The value of G:' + G + '
'
); // Alice will choose the private key a // a is the chosen private key a = 4 ; document . write ( 'The private key a for Alice:' + a + '
'
); // Gets the generated key x = power ( G a P ); // Bob will choose the private key b // b is the chosen private key b = 3 ; document . write ( 'The private key b for Bob:' + b + '
'
); // Gets the generated key y = power ( G b P ); // Generating the secret key after the exchange // of keys ka = power ( y a P ); // Secret key for Alice kb = power ( x b P ); // Secret key for Bob document . write ( 'Secret key for the Alice is:' + ka + '
'
); document . write ( 'Secret key for the Bob is:' + kb + '
'
); // This code is contributed by Ankita saini < /script>

Ausgabe
The value of P : 23 The value of G : 9 The private key a for Alice : 4 The private key b for Bob : 3 Secret key for the Alice is : 9 Secret key for the Bob is : 9