第十一章 非對稱式加密演算法--DH,非對稱式加密演算法--dh
注意:本節內容主要參考自《Java加密與解密的藝術(第2版)》第8章“高等密碼編譯演算法--非對稱式加密演算法”
11.1、非對稱式加密演算法
特點:
- 發送方和接收方均有一個金鑰組(公開金鑰+私密金鑰),其中公開金鑰傳播,私密金鑰自己儲存,不需要傳播
- 私密金鑰不需要傳播的特性解決了對稱式加密演算法中密鑰傳播的困難(這個困難一般通過線下傳遞可以解決)
- 加密安全性極高,只用於一些電子商務網站,加解密速度遠低於對稱式加密
- 一般情況下,為瞭解決非對稱式加密演算法加解密速度低的問題,採用非對稱式加密(使用公開金鑰+私密金鑰對對稱式加密的密鑰進行加解密)+對稱式加密(加解密資料)相結合的方式。
常見演算法:
- DH(非對稱式加密的基石)
- RSA(非對稱式加密的經典,除了可用於非對稱式加密,也可用於數位簽章,RSA--155(512位密鑰)已被破解)
- ElGamal(常用的非對稱式加密演算法)
11.2、DH(僅能用於密鑰分配,不能加解密資料)
實現方式:
- JDK(密鑰長度:512~1024中的64的整數倍)
幾個概念:
- 金鑰組:公開金鑰+私密金鑰
- 本地密鑰:對稱式加密的密鑰
整個流程:
1)甲乙雙方初始化各自的金鑰組
甲方構建出金鑰組keyPair1-->乙方使用甲方的金鑰組中的公開金鑰publicKey1構建出自己的金鑰組keyPair2
2)甲乙雙方構建各自的本地密鑰
甲方使用自己的私密金鑰privateKey1+乙方的公開金鑰publicKey2構建出自己的本地密鑰key1
乙方使用自己的私密金鑰privateKey2+甲方的公開金鑰publicKey1構建出自己的本地密鑰key2
最後會發現key1==key2,這兩個本地密鑰將會是接下來對對稱式加密所使用的密鑰
3)發送方(甲方或乙方均可)使用本地密鑰+對稱式加密演算法對待加密資料進行加密,傳遞給接收方
4)接收方使用本地密鑰+對稱式加密演算法對待解密資料進行解密
1 package com.util.dh; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.InvalidAlgorithmParameterException; 5 import java.security.InvalidKeyException; 6 import java.security.Key; 7 import java.security.KeyFactory; 8 import java.security.KeyPair; 9 import java.security.KeyPairGenerator; 10 import java.security.NoSuchAlgorithmException; 11 import java.security.PrivateKey; 12 import java.security.PublicKey; 13 import java.security.spec.InvalidKeySpecException; 14 import java.security.spec.PKCS8EncodedKeySpec; 15 import java.security.spec.X509EncodedKeySpec; 16 17 import javax.crypto.BadPaddingException; 18 import javax.crypto.Cipher; 19 import javax.crypto.IllegalBlockSizeException; 20 import javax.crypto.KeyAgreement; 21 import javax.crypto.NoSuchPaddingException; 22 import javax.crypto.interfaces.DHPublicKey; 23 import javax.crypto.spec.DHParameterSpec; 24 import javax.crypto.spec.SecretKeySpec; 25 26 import org.apache.commons.codec.binary.Base64; 27 28 /** 29 * 基於JDK的DH演算法,工作模式採用ECB 30 */ 31 public class DHJDK { 32 private static final String ENCODING = "UTF-8"; 33 private static final String FDC_KEY_ALGORITHM = "DH";//非對稱式加密密鑰演算法 34 private static final String DC_KEY_ALGORITHM = "AES";//產生本地密鑰的演算法(對稱式加密密鑰演算法) 35 private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//加解密演算法 格式:演算法/工作模式/填充模式 注意:ECB不使用IV參數 36 private static final int FDC_KEY_SIZE = 512;//非對稱金鑰長度(512~1024之間的64的整數倍) 37 38 /** 39 * 產生甲方金鑰組 40 */ 41 public static KeyPair initKey() throws NoSuchAlgorithmException{ 42 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(FDC_KEY_ALGORITHM);//金鑰組產生器 43 keyPairGenerator.initialize(FDC_KEY_SIZE);//指定密鑰長度 44 KeyPair keyPair = keyPairGenerator.generateKeyPair();//產生金鑰組 45 return keyPair; 46 } 47 48 /** 49 * 產生乙方金鑰組 50 * @param key 甲方公開金鑰 51 */ 52 public static KeyPair initKey(byte[] key) throws NoSuchAlgorithmException, 53 InvalidKeySpecException, 54 InvalidAlgorithmParameterException{ 55 KeyFactory keyFactory = KeyFactory.getInstance(FDC_KEY_ALGORITHM);//密鑰工廠 56 PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(key));//還原甲方公開金鑰 57 DHParameterSpec dHParameterSpec = ((DHPublicKey)publicKey).getParams(); 58 59 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyFactory.getAlgorithm());//乙方金鑰組產生器 60 keyPairGenerator.initialize(dHParameterSpec);//使用甲方公開金鑰參數初始化乙方金鑰組產生器 61 KeyPair keyPair = keyPairGenerator.generateKeyPair();//產生金鑰組 62 return keyPair; 63 } 64 65 /** 66 * DH加密 67 * @param data 帶加密資料 68 * @param keyByte 本地密鑰,由getSecretKey(byte[] publicKey, byte[] privateKey)產生 69 */ 70 public static byte[] encrypt(String data, byte[] keyByte) throws NoSuchAlgorithmException, 71 NoSuchPaddingException, 72 InvalidKeyException, 73 IllegalBlockSizeException, 74 BadPaddingException, 75 UnsupportedEncodingException { 76 Key key = new SecretKeySpec(keyByte, DC_KEY_ALGORITHM);//產生本地密鑰 77 78 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); 79 cipher.init(Cipher.ENCRYPT_MODE, key);//設定加密模式並且初始化key 80 return cipher.doFinal(data.getBytes(ENCODING)); 81 } 82 83 /** 84 * DH解密 85 * @param data 待解密資料為位元組數組 86 * @param keyByte 本地密鑰,由getSecretKey(byte[] publicKey, byte[] privateKey)產生 87 */ 88 public static byte[] decrypt(byte[] data, byte[] keyByte) throws NoSuchAlgorithmException, 89 NoSuchPaddingException, 90 InvalidKeyException, 91 IllegalBlockSizeException, 92 BadPaddingException { 93 Key key = new SecretKeySpec(keyByte, DC_KEY_ALGORITHM);//產生本地密鑰 94 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); 95 cipher.init(Cipher.DECRYPT_MODE, key); 96 return cipher.doFinal(data); 97 } 98 99 /**100 * 根據本方私密金鑰與對方公開金鑰構建本地密鑰(即對稱式加密的密鑰)101 * @param publicKey 對方公開金鑰102 * @param privateKey 本方私密金鑰103 */104 public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws NoSuchAlgorithmException, 105 InvalidKeySpecException, 106 InvalidKeyException{107 KeyFactory keyFactory = KeyFactory.getInstance(FDC_KEY_ALGORITHM);//密鑰工廠108 PublicKey pubkey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));//還原公開金鑰109 PrivateKey prikey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));//還原私密金鑰110 111 KeyAgreement keyAgreement = KeyAgreement.getInstance(keyFactory.getAlgorithm());112 keyAgreement.init(prikey);113 keyAgreement.doPhase(pubkey, true);114 return keyAgreement.generateSecret(DC_KEY_ALGORITHM).getEncoded();//產生本地密鑰(對稱式加密的密鑰)115 }116 117 /**118 * 擷取公開金鑰119 */120 public static byte[] getPublicKey(KeyPair keyPair){121 return keyPair.getPublic().getEncoded();122 }123 124 /**125 * 擷取私密金鑰126 */127 public static byte[] getPrivateKey(KeyPair keyPair){128 return keyPair.getPrivate().getEncoded();129 }130 131 /**132 * 測試133 */134 public static void main(String[] args) throws NoSuchAlgorithmException, 135 InvalidKeySpecException, 136 InvalidAlgorithmParameterException, 137 InvalidKeyException, 138 NoSuchPaddingException, 139 IllegalBlockSizeException, 140 BadPaddingException, 141 UnsupportedEncodingException {142 byte[] pubKey1;//甲方公開金鑰143 byte[] priKey1;//甲方私密金鑰144 byte[] key1;//甲方本地密鑰145 byte[] pubKey2;//乙方公開金鑰146 byte[] priKey2;//乙方私密金鑰147 byte[] key2;//乙方本地密鑰148 149 /*********************測試是否可以正確產生以上6個key,以及key1與key2是否相等*********************/150 KeyPair keyPair1 = DHJDK.initKey();//產生甲方金鑰組151 pubKey1 = DHJDK.getPublicKey(keyPair1);152 priKey1 = DHJDK.getPrivateKey(keyPair1);153 154 KeyPair keyPair2 = DHJDK.initKey(pubKey1);//根據甲方公開金鑰產生乙方金鑰組155 pubKey2 = DHJDK.getPublicKey(keyPair2);156 priKey2 = DHJDK.getPrivateKey(keyPair2);157 158 key1 = DHJDK.getSecretKey(pubKey2, priKey1);//使用對方公開金鑰和自己私密金鑰構建本地密鑰159 key2 = DHJDK.getSecretKey(pubKey1, priKey2);//使用對方公開金鑰和自己私密金鑰構建本地密鑰160 161 System.out.println("甲方公開金鑰pubKey1-->"+Base64.encodeBase64String(pubKey1)+"@@pubKey1.length-->"+pubKey1.length);162 System.out.println("甲方私密金鑰priKey1-->"+Base64.encodeBase64String(priKey1)+"@@priKey1.length-->"+priKey1.length);163 System.out.println("乙方公開金鑰pubKey2-->"+Base64.encodeBase64String(pubKey2)+"@@pubKey2.length-->"+pubKey2.length);164 System.out.println("乙方私密金鑰priKey2-->"+Base64.encodeBase64String(priKey2)+"@@priKey2.length-->"+priKey2.length);165 System.out.println("甲方密鑰key1-->"+Base64.encodeBase64String(key1));166 System.out.println("乙方密鑰key2-->"+Base64.encodeBase64String(key2));167 168 /*********************測試甲方使用本地祕密金鑰加密資料向乙方發送,乙方使用本地密鑰解密資料*********************/169 System.out.println("甲方-->乙方");170 String data = "找一個好姑娘啊!";171 byte[] encodeStr = DHJDK.encrypt(data, key1);172 System.out.println("甲方加密後的資料-->"+Base64.encodeBase64String(encodeStr));173 byte[] decodeStr = DHJDK.decrypt(encodeStr, key2);174 System.out.println("乙方解密後的資料-->"+new String(decodeStr,"UTF-8"));175 176 /*********************測試乙方使用本地祕密金鑰加密資料向甲方發送,甲方使用本地密鑰解密資料*********************/177 System.out.println("乙方-->甲方");178 String data2 = "找一個好姑娘啊!";179 byte[] encodeStr2 = DHJDK.encrypt(data2, key2);180 System.out.println("乙方加密後的資料-->"+Base64.encodeBase64String(encodeStr2));181 byte[] decodeStr2 = DHJDK.decrypt(encodeStr, key1);182 System.out.println("甲方解密後的資料-->"+new String(decodeStr2,"UTF-8"));183 }184 }View Code
注意點:
- 之前說的byte[]數組的比較可以通過依次比較兩個數組中的元素進行比較,當然也可以將兩個數組進行Base64編碼(CC)或十六進位轉換(BC/CC)成字串進行比較
- 測試過程中,自己去測一下key1與key2是否相等,以及甲方發送加密資料,乙方接收資料並解密或者乙方發送加密資料,甲方接收資料並解密
疑問:在我配置非對稱金鑰為512的時候,測出來的公開金鑰長度是228位,私密金鑰長度是213位,與512差很遠,那這裡的512到底是怎麼計算的?(希望知道的朋友給解釋一下)