Diffie-Hellman

  • 0 Respuestas
  • 254 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Conectado Adalher

  • *
  • Underc0der
  • Mensajes: 105
  • Actividad:
    63.33%
  • Reputación 0
    • Ver Perfil

Diffie-Hellman

  • en: Septiembre 01, 2019, 12:25:38 pm
Hola gente,

aquí se escribió una clase para el Diffie-Hellman. El código de demostración más abajo, aquí el código fuente:
Código: Java
  1. import java.math.BigInteger;
  2. import java.security.SecureRandom;
  3.  
  4. /**
  5.  * Utilizes a Diffie-Hellman Key-Exchange<br>
  6.  * <br>
  7.  * <pre>
  8.  * Partner A            Partner B
  9.  * g, p, a (randomly)   b (randomly)
  10.  * A = g^a mod p
  11.  *           g, p, A ==&gt;
  12.  *                      B = g^b mod p
  13.  *                   &lt;== B
  14.  * K = B^a mod p        K = A^b mod p
  15.  * </pre>
  16.  * <br>
  17.  * Example Usage:<br>
  18.  * Partner A:<br>
  19.  * <code>
  20.  * DiffieHellman dh = new DiffieHellman(DiffieHellman.PARTNER_A);<br>
  21.  * dh.init(512);<br>
  22.  * sendToOtherSide(dh.getG());<br>
  23.  * sendToOtherSide(dh.getP());<br>
  24.  * sendToOtherSide(dh.getA());<br>
  25.  * byte[] key = dh.getKey(receiveFromOtherSide());<br>
  26.  * </code><br>
  27.  * <br>
  28.  * Partner B:<br>
  29.  * <code>
  30.  * DiffieHellman dh = new DiffieHellman(DiffieHellman.PARTNER_B);<br>
  31.  * dh.init(512);<br>
  32.  * BigInteger g = receiveFromOtherSide();<br>
  33.  * BigInteger p = receiveFromOtherSide();<br>
  34.  * BigInteger A = receiveFromOtherSide();<br>
  35.  * byte[] key = dh.getKey(g, p, A);<br>
  36.  * sendToOtherSide(dh.getB());<br>
  37.  * </code>
  38.  * @author Tobias Marstaller
  39.  */
  40. public class DiffieHellman
  41. {
  42.     public static final int PARTNER_A = 0xF124A;
  43.     public static final int PARTNER_B = 0xC134B;
  44.    
  45.     protected You are not allowed to view links. Register or Login g;
  46.     protected You are not allowed to view links. Register or Login p;
  47.     protected You are not allowed to view links. Register or Login a;
  48.     protected You are not allowed to view links. Register or Login b;
  49.     protected You are not allowed to view links. Register or Login A;
  50.     protected You are not allowed to view links. Register or Login B;
  51.     protected byte[] K;
  52.     protected int side;
  53.     protected boolean initDone = false;
  54.     protected You are not allowed to view links. Register or Login rand;
  55.    
  56.     /**
  57.      * Constructs a new Key-Exchange for the given side
  58.      */
  59.     public DiffieHellman(int side)
  60.     {
  61.         if (side != PARTNER_A && side != PARTNER_B)
  62.         {
  63.             throw new You are not allowed to view links. Register or Login("side must be equal to PARTNER_A or PARTNER_B");
  64.         }
  65.         this.side = side;
  66.     }
  67.    
  68.     /**
  69.      * Constructs a new Key-Exchange for the given side with the default bit
  70.      * length 1024 and <code>random</code> as a source of randomness.
  71.      * @param random A SPRNG for the parameter-generation
  72.      */
  73.     public DiffieHellman(int side, You are not allowed to view links. Register or Login random)
  74.     {
  75.         this(side);
  76.         this.rand = random;
  77.     }
  78.     /**
  79.      * Constructs a new Key-Exchange for side B (therefore <code>assert(side == PARTNER_B)</code>)
  80.      * with <code>b</code> as the private parameter.<br>
  81.      * If this constructor is used, the call to {@link #init()} is not required anymore.
  82.      * @param b The private parameter <code>b</code>
  83.      */
  84.     public DiffieHellman(int side, You are not allowed to view links. Register or Login b)
  85.     {
  86.         if (side != PARTNER_B)
  87.         {
  88.             throw new You are not allowed to view links. Register or Login("Side must be PARTNER_B");
  89.         }
  90.         this.side = side;
  91.         this.b = b;
  92.         initDone = true;
  93.     }
  94.     /**
  95.      * Constructs a new Key-Exchange for side A (therefore <code>assert(side == PARTNER_A)</code>)
  96.      * with <code>g</code>, <code>p</code> and <code>a</code> as the private parameters.<br>
  97.      * If this constructor is used, the call to {@link #init()} is not required anymore.
  98.      * @param g The private parameter <code>g</code>
  99.      * @param p The private parameter <code>p</code>
  100.      * @param a The private parameter <code>a</code>
  101.      */
  102.     public DiffieHellman(int side, You are not allowed to view links. Register or Login g, You are not allowed to view links. Register or Login p, You are not allowed to view links. Register or Login a)
  103.     {
  104.         if (side != PARTNER_A)
  105.         {
  106.             throw new You are not allowed to view links. Register or Login("Side must be PARTNER_A");
  107.         }
  108.         this.side = side;
  109.         this.g = g;
  110.         this.p = p;
  111.         this.a = a;
  112.         this.A = this.g.modPow(a, p);
  113.         initDone = true;
  114.     }
  115.  
  116.     /**
  117.      * Initializes the instance with the default bit-length 1024
  118.      */
  119.     public void init()
  120.     {
  121.         init(1024);
  122.     }
  123.     /**
  124.      * Initializes the instance with the given bitLength
  125.      */
  126.     public void init(int bitLength)
  127.     {
  128.         if (rand == null)
  129.         {
  130.             rand = new You are not allowed to view links. Register or Login();
  131.         }
  132.         if (this.side == PARTNER_A)
  133.         {
  134.             this.g = You are not allowed to view links. Register or Login.probablePrime(bitLength, rand);
  135.             this.p = You are not allowed to view links. Register or Login.probablePrime(bitLength, rand);
  136.             this.a = You are not allowed to view links. Register or Login.probablePrime(bitLength, rand);
  137.             this.A = this.g.modPow(a, p);
  138.         }
  139.         else if (this.side == PARTNER_B)
  140.         {
  141.             this.b = You are not allowed to view links. Register or Login.probablePrime(bitLength, rand);
  142.         }
  143.         initDone = true;
  144.     }
  145.    
  146.     /**
  147.      * @return Returns the key, if already calculated
  148.      * @throws RuntimeException If the key hast not been calculated yet.
  149.      */
  150.     public byte[] getKey()
  151.     {
  152.         if (this.K == null)
  153.         {
  154.             throw new You are not allowed to view links. Register or Login("The key has not been calculated yet.");
  155.         }
  156.         return K;
  157.     }
  158.     /**
  159.      * Calculates the key for side B (therefore <code>assert(side == PARTNER_B)</code>).
  160.      * @param g Parameter <code>g</code>
  161.      * @param p Parameter <code>p</code>
  162.      * @param A Parameter <code>A</code>
  163.      * @return Returns the calculated key
  164.      * @throws UnsupportedOperaionException If this instance has been inizialied as PARTNER_A
  165.      * @throws RuntimeException If the parameters are not initialized
  166.      */
  167.     public byte[] getKey(You are not allowed to view links. Register or Login g, You are not allowed to view links. Register or Login p, You are not allowed to view links. Register or Login A)
  168.     {
  169.         if (this.side != PARTNER_B)
  170.         {
  171.             throw new You are not allowed to view links. Register or Login("This method only works for PARTNER_B instances.");
  172.         }
  173.         if (!this.initDone)
  174.         {
  175.             throw new You are not allowed to view links. Register or Login("The object has to be initialized.");
  176.         }
  177.         this.B = g.modPow(b, p);
  178.         this.K = A.modPow(b, p).toByteArray();  
  179.         return K;
  180.     }
  181.     /**
  182.      * Calculates the key for side A (therefore <code>assert(side == PARTNER_A)</code>).
  183.      * @param g Parameter <code>g</code>
  184.      * @param p Parameter <code>p</code>
  185.      * @param A Parameter <code>A</code>
  186.      * @return Returns the calculated key
  187.      * @throws UnsupportedOperaionException If this instance has been inizialied as PARTNER_B
  188.      */
  189.     public byte[] getKey(You are not allowed to view links. Register or Login B)
  190.     {
  191.         if (this.side != PARTNER_A)
  192.         {
  193.             throw new You are not allowed to view links. Register or Login("This method only works for PARTNER_A instances.");
  194.         }
  195.         if (!this.initDone)
  196.         {
  197.             throw new You are not allowed to view links. Register or Login("The object has to be initialized.");
  198.         }
  199.         this.K = B.modPow(a, p).toByteArray();
  200.         return K;
  201.     }
  202.  
  203.     public You are not allowed to view links. Register or Login getG()
  204.     {
  205.         return g;
  206.     }
  207.     public You are not allowed to view links. Register or Login getP()
  208.     {
  209.         return p;
  210.     }
  211.     public You are not allowed to view links. Register or Login getA()
  212.     {
  213.         return A;
  214.     }
  215.     public You are not allowed to view links. Register or Login getB()
  216.     {
  217.         return B;
  218.     }
  219. }
  220.  

Código de demostración:
Código: Java
  1. // Partner A
  2. DiffieHellman dh = new DiffieHellman(DiffieHellman.PARTNER_A);
  3. dh.init(512);
  4. sendToOtherSide(dh.getG());
  5. sendToOtherSide(dh.getP());
  6. sendToOtherSide(dh.getA());
  7. byte[] key = dh.getKey(receiveFromOtherSide());
  8.  

Código: Java
  1. // Partner B
  2. DiffieHellman dh = new DiffieHellman(DiffieHellman.PARTNER_B);
  3. dh.init(512);
  4. You are not allowed to view links. Register or Login g = receiveFromOtherSide();
  5. You are not allowed to view links. Register or Login p = receiveFromOtherSide();
  6. You are not allowed to view links. Register or Login A = receiveFromOtherSide();
  7. byte[] key = dh.getKey(g, p, A);
  8. sendToOtherSide(dh.getB());
  9.  

Autor: Tobias Marstaller


Saludos