[英]Diffie-Hellman Key exchange with four parties
我正在嘗試修改三方之間的示例 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 個結果匹配,而第四個結果不同。 我在這里做錯了什么?
更新的答案
我仍然不是密碼學家,所以我在這方面學到了很多東西,謝謝!
我已經重寫了邏輯,整個示例在下面發布,以供以后偶然發現此內容的任何人使用。 它還在沙盒中進行了測試,編譯 Java
我將嘗試解釋這里發生了什么,盡管如果有人閱讀本文發現我的實施/解釋中有任何明顯的缺陷,請告訴我。 我不能說我完全理解它或使用的 Java API 所以我非常感謝任何澄清。
解釋
這使用了一些巧妙的數學運算,其中大部分由底層 API 處理,我們只需要以正確的順序和正確的值告訴它要做什么。
and modulus are chosen and shared between all participants.生成器和模數被選擇並在所有參與者之間共享。
and ) and compute their public keys ( ). Alice、Bob、Carol 和 Sara 選擇私鑰( 和 )並計算他們的公鑰( )。 raised to the power of their private keys, modulo .這些是從提高到他們的私鑰的冪,模計算出來的。
operations using a value passed from another party, where is the number of parties.每一方都需要將他們的操作結果發送給下一方,並且每一方需要使用從另一方傳遞的值執行操作,其中是參與方的數量。
by the power of everyone else's private key modulo , without ever revealing to each other what their private keys are, using a process and pass method.最后,每一方都將通過其他人的私鑰模的冪來提高 ,而無需使用過程和傳遞方法向彼此透露他們的私鑰是什么。
etc) by raising their public keys by the power of the public key of the participant to their left, modulo .在第一次通過時,每個參與者通過將他們的公鑰提高到他們左邊的參與者的公鑰的冪來計算一個中間值( 等),模 。
.在通過 2+ 時,他們重復此過程,但使用左側人的先前操作的結果,將該結果提升到他們自己的私鑰的冪,模 。
這個傳遞和計算過程重復進行,直到每個人計算出 g ABCS ,這成為共享的秘密。
etc由於數學的工作方式(如果我沒記錯的話,指數定律), 等
, but cannot use any combination of these to efficiently reproduce , as they don't know the values of or , as these are the private keys that are never transmitted.竊聽者 Eve 可以看到的值,但不能使用任何組合其中有效地重現 ,因為他們不知道或的值,因為這些是永遠不會傳輸的私鑰。
這在理論上可以擴展到更多的參與者。 按照下面代碼中的模式。 您將添加另一個參與者,將他們添加到每個通道的操作列表中,然后再添加一個通道以確保每個人都在執行所需數量的操作。
我在下面添加了四個和五個參與者的示例代碼。
有四個參與者的示例
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.