第十八篇:JAVA加密解密之DH(Diffie-Hellman)演算法
DH演算法簡介
Diffie-Hellman演算法(D-H演算法),金鑰一致協議。是由公開金鑰密碼體制的奠基人Diffie和Hellman所提出的一種思想。簡單的說就是允許兩名使用者在公開媒體上交換資訊以生成”一致”的、可以共享的金鑰。換句話說,就是由甲方產出一對金鑰(公鑰、私鑰),乙方依照甲方公鑰產生乙方金鑰對(公鑰、私鑰)。以此為基線,作為資料傳輸保密基礎,同時雙方使用同一種對稱加密演算法構建本地金鑰(SecretKey)對資料加密。這樣,在互通了本地金鑰(SecretKey)演算法後,甲乙雙方公開自己的公鑰,使用對方的公鑰和剛才產生的私鑰加密資料,同時可以使用對方的公鑰和自己的私鑰對資料解密。不單單是甲乙雙方兩方,可以擴充套件為多方共享資料通訊,這樣就完成了網路互動資料的安全通訊!該演算法源於中國的同餘定理——中國餘數定理。
- 甲方構建金鑰對兒,將公鑰公佈給乙方,將私鑰保留;雙方約定資料加密演算法;乙方通過甲方公鑰構建金鑰對兒,將公鑰公佈給甲方,將私鑰保留。
- 甲方使用私鑰、乙方公鑰、約定資料加密演算法構建本地金鑰,然後通過本地金鑰加密資料,傳送給乙方加密後的資料;乙方使用私鑰、甲方公鑰、約定資料加密演算法構建本地金鑰,然後通過本地金鑰對資料解密。
- 乙方使用私鑰、甲方公鑰、約定資料加密演算法構建本地金鑰,然後通過本地金鑰加密資料,傳送給甲方加密後的資料;甲方使用私鑰、乙方公鑰、約定資料加密演算法構建本地金鑰,然後通過本地金鑰對資料解密。
DH演算法實現
package com.jianggujin.codec;
import java.security.KeyPair;
/**
* 金鑰對
*
* @author jianggujin
*
*/
public class HQKeyPair
{
private byte[] privateKey;
private byte[] publicKey;
public HQKeyPair()
{
}
public HQKeyPair(KeyPair keyPair)
{
this.privateKey = keyPair.getPrivate().getEncoded();
this.publicKey = keyPair.getPublic().getEncoded();
}
public HQKeyPair(byte[] privateKey, byte[] publicKey)
{
this.privateKey = privateKey;
this.publicKey = publicKey;
}
public byte[] getPrivateKey()
{
return privateKey;
}
public void setPrivateKey(byte[] privateKey)
{
this.privateKey = privateKey;
}
public byte[] getPublicKey()
{
return publicKey;
}
public void setPublicKey(byte[] publicKey)
{
this.publicKey = publicKey;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
package com.jianggujin.codec;
import java.security.InvalidAlgorithmParameterException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
/**
* Diffie-Hellman演算法(D-H演算法),金鑰一致協議。是由公開金鑰密碼體制的奠基人Diffie和Hellman所提出的一種思想。
*
* @author jianggujin
*
*/
public class HQDH
{
private static HQDH dh = new HQDH();
public static HQDH getInstance()
{
return dh;
}
private HQDH()
{
}
/**
* 對稱演算法
*
* @author jianggujin
*
*/
public static enum HQDHSymmetricalAlgorithm
{
DES("DES"), DESede("DESede");
private String name;
private HQDHSymmetricalAlgorithm(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
private final String ALGORITHM = "DH";
/**
* 初始化甲方金鑰
*
* @return
* @throws NoSuchAlgorithmException
*/
public HQKeyPair initPartyAKey() throws NoSuchAlgorithmException
{
return initPartyAKey(1024);
}
/**
* 初始化甲方金鑰
*
* @param keySize
* @return
* @throws NoSuchAlgorithmException
*/
public HQKeyPair initPartyAKey(int keySize) throws NoSuchAlgorithmException
{
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
return new HQKeyPair(keyPair);
}
/**
* 初始化乙方金鑰
*
* @param partyAPublicKey
* 甲方公鑰
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws InvalidAlgorithmParameterException
* @throws Exception
*/
public HQKeyPair initPartyBKey(byte[] partyAPublicKey) throws Exception
{
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(partyAPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
// 由甲方公鑰構建乙方金鑰
DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams();
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyFactory.getAlgorithm());
keyPairGenerator.initialize(dhParamSpec);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return new HQKeyPair(keyPair);
}
public byte[] encrypt(byte[] data, byte[] publicKey, byte[] privateKey, HQDHSymmetricalAlgorithm algorithm)
throws Exception
{
return encrypt(data, publicKey, privateKey, algorithm.getName());
}
public byte[] encrypt(byte[] data, byte[] publicKey, byte[] privateKey, String algorithm) throws Exception
{
// 生成本地金鑰
SecretKey secretKey = getSecretKey(publicKey, privateKey, algorithm);
// 資料加密
Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
public byte[] decrypt(byte[] data, byte[] publicKey, byte[] privateKey, HQDHSymmetricalAlgorithm algorithm)
throws Exception
{
return decrypt(data, publicKey, privateKey, algorithm.getName());
}
public byte[] decrypt(byte[] data, byte[] publicKey, byte[] privateKey, String algorithm) throws Exception
{
// 生成本地金鑰
SecretKey secretKey = getSecretKey(publicKey, privateKey, algorithm);
// 資料解密
Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
private SecretKey getSecretKey(byte[] publicKey, byte[] privateKey, String algorithm) throws Exception
{
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
Key priKey = keyFactory.generatePrivate(pkcs8KeySpec);
KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
keyAgree.init(priKey);
keyAgree.doPhase(pubKey, true);
// 生成本地金鑰
SecretKey secretKey = keyAgree.generateSecret(algorithm);
return secretKey;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
測試程式碼:
import org.junit.Test;
import com.jianggujin.codec.HQBase64;
import com.jianggujin.codec.HQDH;
import com.jianggujin.codec.HQDH.HQDHSymmetricalAlgorithm;
import com.jianggujin.codec.HQKeyPair;
public class DHTest
{
HQDH dh = HQDH.getInstance();
HQBase64 base64 = HQBase64.getInstance();
@Test
public void encode() throws Exception
{
byte[] data = "jianggujin".getBytes();
HQKeyPair keyPairA = dh.initPartyAKey();
System.err.println("甲方私鑰:" + base64.encodeToString(keyPairA.getPrivateKey()));
System.err.println("甲方公鑰:" + base64.encodeToString(keyPairA.getPublicKey()));
HQKeyPair keyPairB = dh.initPartyBKey(keyPairA.getPublicKey());
System.err.println("乙方私鑰:" + base64.encodeToString(keyPairB.getPrivateKey()));
System.err.println("乙方公鑰:" + base64.encodeToString(keyPairB.getPublicKey()));
HQDHSymmetricalAlgorithm[] algorithms = HQDHSymmetricalAlgorithm.values();
for (HQDHSymmetricalAlgorithm algorithm : algorithms)
{
System.err.println("=========================================");
System.err.println(algorithm);
byte[] result = dh.encrypt(data, keyPairB.getPublicKey(), keyPairA.getPrivateKey(), algorithm);
System.err.println("加密:" + base64.encodeToString(result));
System.err.println(
"解密:" + new String(dh.decrypt(result, keyPairA.getPublicKey(), keyPairB.getPrivateKey(), algorithm)));
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
測試結果:
甲方私鑰:MIIBZwIBADCCARsGCSqGSIb3DQEDATCCAQwCgYEA/X9TgR11EilS30qcLuzk5/YRt1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQIsJPu6nX/rfGG/g7V+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rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAccCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoCAgIABEMCQQCbq4VyWTHlIbhxqIMAsycz+J6wyxs41MsehQhZC1dv4z9BHzDDBtRG5JDgDMEh2ABkLKO0sXyZSzsW34442GPN
乙方公鑰:MIIBpjCCARsGCSqGSIb3DQEDATCCAQwCgYEA/X9TgR11EilS30qcLuzk5/YRt1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQIsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAccCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoCAgIAA4GEAAKBgF1+TvPiAefyWJJ2Or7TNGlk/oiT94pUyMrdXmSpgdf4AOwB9MgvZQvmlwx+hKrEFBKKcdUnfsZffdj2XzlMx46IWErUT9+NfbaLfuMTRmH8ZMdfXxHckoJzcC6CFrVR2DoDkzM2BCUrEy7ArxFoWoL27xsIcb1PC+4wNV1ah8JQ =========================================
DES
加密:Oxk3L0a0C990XyyWvj5Mlg==
解密:jianggujin =========================================
DESede
加密:VtrJvgsxnAY7cd3alRUwPA==
解密:jianggujin