小編給大家分享一下java中密鑰交換算法DH怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)門頭溝免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了超過千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
具體如下:
一 對稱加密缺點
密鑰傳遞過程復(fù)雜,這是對稱加密帶來的困擾。
二 DH密鑰交換算法特點
構(gòu)建本地密鑰
雙方密鑰一致
三 DH相關(guān)參數(shù)
四 DH算法實現(xiàn)過程
1、初始化發(fā)送方的密鑰(KeyPairGenerator、KeyPair、PublicKey)
2、初始化接受方的密鑰(KeyFactory、X509EncodedKeySpec、DHPublicKey、DHParameterSpec、KeyPairGenerator、PrivateKey)
3、密鑰構(gòu)建(KeyAgeement、SecretKey、KeyFactory、X509EncodeKeySpec、PublicKey)
4、加密和解密(Cipher)
五 DH代碼實現(xiàn)過程
package com.imooc.security.dh; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; 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; import org.apache.commons.codec.binary.Base64; //import com.sun.org.apache.xalan.internal.utils.Objects; public class ImoocDH { private static String src = "cakin24 security dh"; public static void main(String[] args) { jdkDH(); } public static void jdkDH() { try { //1.初始化發(fā)送方密鑰 KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance("DH"); senderKeyPairGenerator.initialize(512); KeyPair senderKeyPair = senderKeyPairGenerator.generateKeyPair(); byte[] senderPublicKeyEnc = senderKeyPair.getPublic().getEncoded();//發(fā)送方公鑰,發(fā)送給接收方(網(wǎng)絡(luò)、文件。。。) //2.初始化接收方密鑰 KeyFactory receiverKeyFactory = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(senderPublicKeyEnc); PublicKey receiverPublicKey = receiverKeyFactory.generatePublic(x509EncodedKeySpec); DHParameterSpec dhParameterSpec = ((DHPublicKey)receiverPublicKey).getParams(); KeyPairGenerator receiverKeyPairGenerator = KeyPairGenerator.getInstance("DH"); receiverKeyPairGenerator.initialize(dhParameterSpec); KeyPair receiverKeypair = receiverKeyPairGenerator.generateKeyPair(); PrivateKey receiverPrivateKey = receiverKeypair.getPrivate(); byte[] receiverPublicKeyEnc = receiverKeypair.getPublic().getEncoded(); //3.密鑰構(gòu)建 KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH"); receiverKeyAgreement.init(receiverPrivateKey); receiverKeyAgreement.doPhase(receiverPublicKey, true); SecretKey receiverDesKey = receiverKeyAgreement.generateSecret("DES"); KeyFactory senderKeyFactory = KeyFactory.getInstance("DH"); x509EncodedKeySpec = new X509EncodedKeySpec(receiverPublicKeyEnc); PublicKey senderPublicKey = senderKeyFactory.generatePublic(x509EncodedKeySpec); KeyAgreement senderKeyAgreement = KeyAgreement.getInstance("DH"); senderKeyAgreement.init(senderKeyPair.getPrivate()); senderKeyAgreement.doPhase(senderPublicKey, true); SecretKey senderDesKey = senderKeyAgreement.generateSecret("DES"); //if (Objects.equals(receiverDesKey, senderDesKey)) { if(receiverDesKey.equals(senderDesKey)){ System.out.println("雙方密鑰相同"); } //4.加密 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, senderDesKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk dh encrypt : " + Base64.encodeBase64String(result)); //5.解密 cipher.init(Cipher.DECRYPT_MODE, receiverDesKey); result = cipher.doFinal(result); System.out.println("jdk dh decrypt : " + new String(result)); } catch (Exception e) { e.printStackTrace(); } } }
六 實現(xiàn)效果
雙方密鑰相同
jdk dh encrypt : wqnKvpQve05UD64OCQGXcQu5rcssEVSu
jdk dh decrypt : cakin24 security dh
七 應(yīng)用場景
以上是“java中密鑰交換算法DH怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!