迪菲-赫尔曼钥匙与四方交流



我正在尝试修改三方之间的Diffie-Hellman密钥交换示例。

这是代码。

           
       // Alice uses Carol's public key
            Key ac = aliceKeyAgree.doPhase(carolKpair.getPublic(), false);
        // Bob uses Alice's public key
            Key ba = bobKeyAgree.doPhase(aliceKpair.getPublic(), false);
        // Carol uses Bob's public key
            Key cb = carolKeyAgree.doPhase(bobKpair.getPublic(), false);
            
            Key sc = saraKeyAgree.doPhase(carolKpair.getPublic(), false);
            
        // Alice uses Carol's result from above
            aliceKeyAgree.doPhase(cb, true);
        // Bob uses Alice's result from above
            bobKeyAgree.doPhase(ac, true);
        // Carol uses Bob's result from above
            carolKeyAgree.doPhase(ba, true);
            saraKeyAgree.doPhase(sc,true);
            
        // Alice, Bob and Carol compute their secrets
            byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
            System.out.println("Alice secret: " + toHexString(aliceSharedSecret));
            byte[] bobSharedSecret = bobKeyAgree.generateSecret();
            System.out.println("Bob secret: " + toHexString(bobSharedSecret));
            byte[] carolSharedSecret = carolKeyAgree.generateSecret();
            System.out.println("Carol secret: " + toHexString(carolSharedSecret));
            
            byte[] saraSharedSecret = saraKeyAgree.generateSecret();
            System.out.println("Sara secret: " + toHexString(saraSharedSecret));
        // Compare Alice and Bob
            if (!java.util.Arrays.equals(aliceSharedSecret, bobSharedSecret))
                throw new Exception("Alice and Bob differ");
            System.out.println("Alice and Bob are the same");
        // Compare Bob and Carol
            if (!java.util.Arrays.equals(bobSharedSecret, carolSharedSecret))
                throw new Exception("Bob and Carol differ");
            System.out.println("Bob and Carol are the same");

最后,只有 3 个结果匹配,第四个结果不同。我在这里做错了什么?

更新的答案

我仍然不是密码学家,所以我从中学到了很多东西,谢谢!

我已经重写了逻辑,整个示例发布在下面,供以后偶然发现的任何人使用。它也在Compile Java的沙箱中进行了测试。

我将尝试解释这里发生的事情,尽管如果阅读本文的人注意到我的实现/解释中存在任何巨大的缺陷,请告诉我。我不能说我完全理解它或使用的Java API,所以我非常感谢任何澄清。

解释

这使用了一些聪明的数学,其中大部分由底层 API 处理,我们只需要告诉它该做什么,以正确的顺序和正确的值。

选择生成器g和模数p并在所有参与者之间共享。

Alice,Bob,Carol和Sara选择私钥(A,B,CS(并计算他们的公钥(g A,gB,g C,gS(。这些是从g提高到它们的私钥的幂,模p计算出来的。

每一方都需要将其操作的结果发送给下一方,每一方都需要使用从另一方传递的值执行 n-1 个操作,其中 n 是参与方数。

最后,每一方都会通过其他人的私钥模 p 的力量来提升 g,而不会使用进程和传递方法向对方透露他们的私钥是什么。

在第一次传递中,每个参与者通过参与者的公钥的幂提高他们的公钥来计算一个中间值(g AB、gBC 等(,模 p

在 pass 2+ 上,他们重复此过程,但使用左侧人员的先前操作的结果,将该结果提升到他们自己的私钥模 p 的幂。

此传递和计算过程将重复,直到每个人都计算出g ABCS,这将成为共享机密。

由于数学的工作方式(如果我没记错的话,指数定律(, g ABCS = gBCSA = gCSAB = gSABC

Evesdropper,Eve将能够看到g A,g B,g C,g S,g AB,g BC,g CS,gSA,g ABC,g BCS,gCSA的值,但不能使用这些组合来有效地复制gABCS,因为他们不知道A,B,CS的值 因为这些是永远不会传输的私钥。

从理论上讲,这可以扩展到更多的参与者。按照以下代码中的模式进行操作。您将添加另一个参与者,将他们添加到每个传递中的操作列表中,然后再添加一个传递,以确保每个人都在执行所需数量的操作。

我在下面为四位和五位参与者添加了示例代码。

具有四个参与者的示例

import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
   /*
    * This program executes the Diffie-Hellman key agreement protocol between
    * 4 parties: Alice, Bob, Carol and Sara using a shared 2048-bit DH parameter.
    */
    public class DHKeyAgreement4 {
        private DHKeyAgreement4() {}
        public static void main(String argv[]) throws Exception {

        // Alice creates her own DH key pair with 2048-bit key size
            KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
            aliceKpairGen.initialize(2048);
            KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
        // This DH parameters can also be constructed by creating a
        // DHParameterSpec object using agreed-upon values
            DHParameterSpec dhParamShared = ((DHPublicKey)aliceKpair.getPublic()).getParams();
        // Bob creates his own DH key pair using the same params
            KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");
            bobKpairGen.initialize(dhParamShared);
            KeyPair bobKpair = bobKpairGen.generateKeyPair();
        // Carol creates her own DH key pair using the same params
            KeyPairGenerator carolKpairGen = KeyPairGenerator.getInstance("DH");
            carolKpairGen.initialize(dhParamShared);
            KeyPair carolKpair = carolKpairGen.generateKeyPair();
        // Carol creates her own DH key pair using the same params
            KeyPairGenerator saraKpairGen = KeyPairGenerator.getInstance("DH");
            saraKpairGen.initialize(dhParamShared);
            KeyPair saraKpair = saraKpairGen.generateKeyPair();


          //Alice initialize
          KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
          //Alice computes gA
          aliceKeyAgree.init(aliceKpair.getPrivate());
          //Bob initialize
          KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
          //Bob computes gB
          bobKeyAgree.init(bobKpair.getPrivate());
          //Carol initialize
          KeyAgreement carolKeyAgree = KeyAgreement.getInstance("DH");
          //Carol computes gC
          carolKeyAgree.init(carolKpair.getPrivate());
          //Sara initialize
          KeyAgreement saraKeyAgree = KeyAgreement.getInstance("DH");
          //Sara computes gS
          saraKeyAgree.init(saraKpair.getPrivate());

          //First Pass
          //Alice computes gSA
          Key gSA = aliceKeyAgree.doPhase(saraKpair.getPublic(), false);
          //Bob computes gAB
          Key gAB = bobKeyAgree.doPhase(aliceKpair.getPublic(), false); 
          //Carol computes gBC
          Key gBC = carolKeyAgree.doPhase(bobKpair.getPublic(), false); 
          //Sara computes gCS
          Key gCS = saraKeyAgree.doPhase(carolKpair.getPublic(), false);

          //Second Pass
          //Alice computes gCSA
          Key gCSA = aliceKeyAgree.doPhase(gCS, false);
          //Bob computes gSAB
          Key gSAB = bobKeyAgree.doPhase(gSA, false);
          //Carol computes gABC
          Key gABC = carolKeyAgree.doPhase(gAB, false);
          //Sara computes gBCS
          Key gBCS = saraKeyAgree.doPhase(gBC, false); 

          //Third Pass
          //Alice computes gBCSA
          Key gBCSA = aliceKeyAgree.doPhase(gBCS, true); //This is Alice's secret
          //Bob computes gCSAB
          Key gCSAB = bobKeyAgree.doPhase(gCSA, true); //This is Bob's secret
          //Sara Computes gABCS
          Key gABCS = saraKeyAgree.doPhase(gABC, true); //This is Sara's secret
          //Carol computes gSABC
          Key gSABC = carolKeyAgree.doPhase(gSAB, true); //This is Carol's secret

        // Alice, Bob, Carol and Sara compute their secrets
            byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
            System.out.println("Alice secret: " + toHexString(aliceSharedSecret));
            byte[] bobSharedSecret = bobKeyAgree.generateSecret();
            System.out.println("Bob secret: " + toHexString(bobSharedSecret));
            byte[] carolSharedSecret = carolKeyAgree.generateSecret();
            System.out.println("Carol secret: " + toHexString(carolSharedSecret));
            byte[] saraSharedSecret = saraKeyAgree.generateSecret();
            System.out.println("Sara secret: " + toHexString(saraSharedSecret));
        // Compare Alice and Bob
            if (!java.util.Arrays.equals(aliceSharedSecret, bobSharedSecret))
                System.out.println("Alice and Bob differ");//    throw new Exception("Alice and Bob differ");
            else
                System.out.println("Alice and Bob are the same");
        // Compare Bob and Carol
            if (!java.util.Arrays.equals(bobSharedSecret, carolSharedSecret))
                System.out.println("Bob and Carol differ");//throw new Exception("Bob and Carol differ");
            else
                System.out.println("Bob and Carol are the same");
          //Compare Carol and Sara
            if (!java.util.Arrays.equals(carolSharedSecret, saraSharedSecret))
                System.out.println("Carol and Sara differ");//throw new Exception("Carol and Sara differ");
            else
                System.out.println("Carol and Sara are the same");
        }
    /*
     * Converts a byte to hex digit and writes to the supplied buffer
     */
        private static void byte2hex(byte b, StringBuffer buf) {
            char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            int high = ((b & 0xf0) >> 4);
            int low = (b & 0x0f);
            buf.append(hexChars[high]);
            buf.append(hexChars[low]);
        }
    /*
     * Converts a byte array to hex string
     */
        private static String toHexString(byte[] block) {
            StringBuffer buf = new StringBuffer();
            int len = block.length;
            for (int i = 0; i < len; i++) {
                byte2hex(block[i], buf);
                if (i < len-1) {
                    buf.append(":");
                }
            }
            return buf.toString();
        }
    }

有五个参与者的示例

import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
   /*
    * This program executes the Diffie-Hellman key agreement protocol between
    * 5 parties: Alice, Bob, Carol, Sara and Dave using a shared 2048-bit DH parameter.
    */
    public class DHKeyAgreement5 {
        private DHKeyAgreement5() {}
        public static void main(String argv[]) throws Exception {

        // Alice creates her own DH key pair with 2048-bit key size
            KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
            aliceKpairGen.initialize(2048);
            KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
        // This DH parameters can also be constructed by creating a
        // DHParameterSpec object using agreed-upon values
            DHParameterSpec dhParamShared = ((DHPublicKey)aliceKpair.getPublic()).getParams();
        // Bob creates his own DH key pair using the same params
            KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");
            bobKpairGen.initialize(dhParamShared);
            KeyPair bobKpair = bobKpairGen.generateKeyPair();
        // Carol creates her own DH key pair using the same params
            KeyPairGenerator carolKpairGen = KeyPairGenerator.getInstance("DH");
            carolKpairGen.initialize(dhParamShared);
            KeyPair carolKpair = carolKpairGen.generateKeyPair();
        // Sara creates her own DH key pair using the same params
            KeyPairGenerator saraKpairGen = KeyPairGenerator.getInstance("DH");
            saraKpairGen.initialize(dhParamShared);
            KeyPair saraKpair = saraKpairGen.generateKeyPair();
                    // Dave creates her own DH key pair using the same params
            KeyPairGenerator daveKpairGen = KeyPairGenerator.getInstance("DH");
            daveKpairGen.initialize(dhParamShared);
            KeyPair daveKpair = daveKpairGen.generateKeyPair();

          //Alice initialize
          KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
          //Alice computes gA
          aliceKeyAgree.init(aliceKpair.getPrivate());
          //Bob initialize
          KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
          //Bob computes gB
          bobKeyAgree.init(bobKpair.getPrivate());
          //Carol initialize
          KeyAgreement carolKeyAgree = KeyAgreement.getInstance("DH");
          //Carol computes gC
          carolKeyAgree.init(carolKpair.getPrivate());
          //Sara initialize
          KeyAgreement saraKeyAgree = KeyAgreement.getInstance("DH");
          //Sara computes gS
          saraKeyAgree.init(saraKpair.getPrivate());
          //Dave initialize
          KeyAgreement daveKeyAgree = KeyAgreement.getInstance("DH");
          //Sara computes gS
          daveKeyAgree.init(daveKpair.getPrivate());

          //First Pass
          //Alice computes gDA
          Key gDA = aliceKeyAgree.doPhase(daveKpair.getPublic(), false);
          //Bob computes gAB
          Key gAB = bobKeyAgree.doPhase(aliceKpair.getPublic(), false); 
          //Carol computes gBC
          Key gBC = carolKeyAgree.doPhase(bobKpair.getPublic(), false); 
          //Sara computes gCS
          Key gCS = saraKeyAgree.doPhase(carolKpair.getPublic(), false);
          //Dave computed gSD
          Key gSD = daveKeyAgree.doPhase(saraKpair.getPublic(), false);

          //Second Pass
          //Alice computes gSDA
          Key gSDA = aliceKeyAgree.doPhase(gSD, false);
          //Bob computes gDAB
          Key gDAB = bobKeyAgree.doPhase(gDA, false);
          //Carol computes gABC
          Key gABC = carolKeyAgree.doPhase(gAB, false);
          //Sara computes gBCS
          Key gBCS = saraKeyAgree.doPhase(gBC, false); 
          //Dave computes gCSD
          Key gCSD = daveKeyAgree.doPhase(gCS, false); 
          //Third Pass
          //Alice computes gCSDA
          Key gCSDA = aliceKeyAgree.doPhase(gCSD, false); 
          //Bob computes gSDAB
          Key gSDAB = bobKeyAgree.doPhase(gSDA, false); 
          //Carol computes gDABC
          Key gDABC = carolKeyAgree.doPhase(gDAB, false); 
          //Sara Computes gABCS
          Key gABCS = saraKeyAgree.doPhase(gABC, false); 
          //Dave computes gBCSC
          Key gBCSD = daveKeyAgree.doPhase(gBCS, false); 
          //Fourth Pass
          //Alice computes gBCSDA
          Key gBCSDA = aliceKeyAgree.doPhase(gBCSD, true); //This is Alice's secret
          //Bob computes gSDABC
          Key gCSDAB = bobKeyAgree.doPhase(gCSDA, true); //This is Bob's secret
          //Carol computes gSABC
          Key gSDABC = carolKeyAgree.doPhase(gSDAB, true); //This is Carol's secret
          //Sara Computes gABCS
          Key gDABCS = saraKeyAgree.doPhase(gDABC, true); //This is Sara's secret

          Key gABCSD = daveKeyAgree.doPhase(gABCS, true); //This is Dave's secret

        // Alice, Bob, Carol and Sara compute their secrets
            byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
            System.out.println("Alice secret: " + toHexString(aliceSharedSecret));
            byte[] bobSharedSecret = bobKeyAgree.generateSecret();
            System.out.println("Bob secret: " + toHexString(bobSharedSecret));
            byte[] carolSharedSecret = carolKeyAgree.generateSecret();
            System.out.println("Carol secret: " + toHexString(carolSharedSecret));
            byte[] saraSharedSecret = saraKeyAgree.generateSecret();
            System.out.println("Sara secret: " + toHexString(saraSharedSecret));
          byte[] daveSharedSecret = daveKeyAgree.generateSecret();
            System.out.println("Dave secret: " + toHexString(daveSharedSecret));
        // Compare Alice and Bob
            if (!java.util.Arrays.equals(aliceSharedSecret, bobSharedSecret))
                System.out.println("Alice and Bob differ");//    throw new Exception("Alice and Bob differ");
            else
                System.out.println("Alice and Bob are the same");
        // Compare Bob and Carol
            if (!java.util.Arrays.equals(bobSharedSecret, carolSharedSecret))
                System.out.println("Bob and Carol differ");//throw new Exception("Bob and Carol differ");
            else
                System.out.println("Bob and Carol are the same");
          //Compare Carol and Sara
            if (!java.util.Arrays.equals(carolSharedSecret, saraSharedSecret))
                System.out.println("Carol and Sara differ");//throw new Exception("Carol and Sara differ");
            else
                System.out.println("Carol and Sara are the same");
          //Compare Sara and Dave
          if (!java.util.Arrays.equals(saraSharedSecret, daveSharedSecret))
                System.out.println("Sara and Dave differ");//throw new Exception("Carol and Sara differ");
            else
                System.out.println("Sara and Dave are the same");
        }
    /*
     * Converts a byte to hex digit and writes to the supplied buffer
     */
        private static void byte2hex(byte b, StringBuffer buf) {
            char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            int high = ((b & 0xf0) >> 4);
            int low = (b & 0x0f);
            buf.append(hexChars[high]);
            buf.append(hexChars[low]);
        }
    /*
     * Converts a byte array to hex string
     */
        private static String toHexString(byte[] block) {
            StringBuffer buf = new StringBuffer();
            int len = block.length;
            for (int i = 0; i < len; i++) {
                byte2hex(block[i], buf);
                if (i < len-1) {
                    buf.append(":");
                }
            }
            return buf.toString();
        }
    }

相关内容

  • 没有找到相关文章

最新更新