3.1 DH算法簡(jiǎn)介

非對(duì)稱加密算法 - DH算法

DH算法是非對(duì)稱加密算法的鼻祖,為非對(duì)稱加密算法奠定了基礎(chǔ),主要用途是進(jìn)行密鑰交換。

DH算法歷史

1976年非對(duì)稱加密算法思想被提出,但是當(dāng)時(shí)并沒有給出具體算法和方案,因?yàn)楫?dāng)時(shí)沒有研究出單向函數(shù)(也就是信息摘要算法還沒出現(xiàn)),但是IEEE的期刊(作者:W.Diffie和M.Hellman)中給出了通信時(shí)雙方如何通過信息交換協(xié)商密鑰的算法,也就是DH算法,通過該算法雙方可以協(xié)商對(duì)稱加密的密鑰。

DH算法的目的僅在于<b>雙方在安全的環(huán)境下協(xié)商一個(gè)加解密的密鑰</b>,因此僅僅用于密鑰分配,不能用于加解密消息。

應(yīng)用場(chǎng)景

僅用于密鑰交換場(chǎng)景,不適用于數(shù)據(jù)傳輸?shù)募咏饷埽鏏B兩個(gè)系統(tǒng)需要交換密鑰,則過程如下:

  • A系統(tǒng)構(gòu)建密鑰:構(gòu)建一對(duì)公私密鑰Private Key1和Public Key1;
  • A系統(tǒng)向B系統(tǒng)公布自己的公鑰(Public Key1);
  • B系統(tǒng)使用A公布的公鑰(Public Key1)建立一對(duì)密鑰:Private Key2和Public Key2;
  • B系統(tǒng)向A系統(tǒng)公布自己的公鑰Public Key2;
  • A系統(tǒng)使用自己的私鑰Private Key1和B系統(tǒng)的公鑰Public Key2構(gòu)建本地密鑰;
  • B系統(tǒng)使用自己的私鑰Private Key2和A系統(tǒng)的公鑰Public Key1構(gòu)建本地密鑰;

關(guān)鍵點(diǎn):B系統(tǒng)使用A系統(tǒng)的公鑰建立加密用的Key;

本地密鑰用來加解密數(shù)據(jù);

雖然AB系統(tǒng)使用了不同的密鑰建立自己的本地密鑰,但是AB系統(tǒng)獲得本地密鑰是一致的。

流程描述

sequenceDiagram
A->> A: 構(gòu)建密鑰對(duì):private key1 和 public key1
A->> B: 公布自己的公鑰: public key1
B->> B: 使用public key1構(gòu)建自己的密鑰對(duì) private key2 和 public key2;
B-->> A: 返回自己的public key2;
A->> A: 使用private key1 和 public key2 構(gòu)建本地密鑰;
B->> B : 使用private key2 和 public key1構(gòu)建本地密鑰;

Java中算法實(shí)現(xiàn)

Java提供DH算法的實(shí)現(xiàn),不用使用第三方開源包,DH算法的產(chǎn)生的密鑰長(zhǎng)度在512到1024之間,必須是64的倍數(shù),默認(rèn)是1024。

A系統(tǒng)建立自己的密鑰對(duì)

此過程不需要參數(shù)

/***
     * A系統(tǒng)產(chǎn)生密鑰,這個(gè)過程不需要參數(shù),由DH算法計(jì)算得出<br/>
     * 內(nèi)部使用一些安全的隨機(jī)函數(shù)隨機(jī)計(jì)算出一個(gè)公私鑰<br>
     * 計(jì)算后的公私鑰要存儲(chǔ)下來,存儲(chǔ)二進(jìn)制數(shù)據(jù)
     * 
     * @return
     * @throws Exception
     */
    public static Map<String, Key> initASysKey() throws Exception {
        // 使用DH算法生成公司密鑰
        KeyPairGenerator keyPairGr = KeyPairGenerator.getInstance(KEY_ALGORITH);
        keyPairGr.initialize(KEY_SIZE);
        KeyPair keyPair = keyPairGr.generateKeyPair();

        // 可以使用DB算法專業(yè)密鑰前行轉(zhuǎn)換獲取的PublicKey和PrivateKey
        // DHPublicKey dhPK = (DHPublicKey) publicKey;
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // 存儲(chǔ),傳輸密鑰
        Map<String, Key> keyMap = new HashMap<String, Key>(2);
        keyMap.put(MAP_KEY_PUBLIC, publicKey);
        keyMap.put(MAP_KEY_PRIVATGE, privateKey);
        return keyMap;
    }

B建立自己的密鑰對(duì)

需要A系統(tǒng)發(fā)送自己的公鑰給B后,B才能建立自己的密鑰對(duì):

/**
     * B構(gòu)建自己的公私鑰,要使用A的公鑰構(gòu)建<br>
     * DH算法接受公鑰,并構(gòu)建自己的密鑰對(duì)<br>
     * 這個(gè)過程中用到了一些密鑰格式轉(zhuǎn)換對(duì)象,不是重點(diǎn)<br>
     * B也要保持自己的密鑰對(duì),二進(jìn)制形式
     * 
     * @param pubKey
     * @return
     * @throws Exception
     */
    public static Map<String, Key> initBSysKey(byte[] pubKey) throws Exception {

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITH);
        // 密鑰格式轉(zhuǎn)換對(duì)象
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
        // 轉(zhuǎn)換PublicKey格式
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
        // 構(gòu)建DH算法參數(shù)
        DHParameterSpec dhParamSpec = ((DHPublicKey) publicKey).getParams();
        // 使用DH算法創(chuàng)建密鑰對(duì)
        KeyPairGenerator keyPairGr = KeyPairGenerator.getInstance(keyFactory.getAlgorithm());
        keyPairGr.initialize(dhParamSpec);
        KeyPair keyPair = keyPairGr.generateKeyPair();
        // 創(chuàng)建的公私鑰
        DHPublicKey publicKey1 = (DHPublicKey) keyPair.getPublic();
        DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
        // 存儲(chǔ)傳輸密鑰
        Map<String, Key> keyMap = new HashMap<String, Key>(2);
        keyMap.put(MAP_KEY_PUBLIC, publicKey1);
        keyMap.put(MAP_KEY_PRIVATGE, privateKey);
        return keyMap;
    }

AB分別建立自己的本地密鑰

A、B系統(tǒng)都需要對(duì)方已經(jīng)發(fā)送了公鑰給自己,使用自己的私鑰和對(duì)方的公鑰建立密鑰對(duì)。

/**
     * 使用對(duì)方的公鑰和自己的私鑰構(gòu)建對(duì)稱加密的SecretKey<br>
     * 
     * @param publicKey
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
        // 建立密鑰工廠
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITH);
        // 密鑰編碼轉(zhuǎn)換對(duì)象
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
        // 轉(zhuǎn)換公鑰格式
        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
        // 密鑰編碼轉(zhuǎn)換對(duì)象
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
        // 轉(zhuǎn)換私鑰格式
        PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
        // 利用公鑰和私鑰創(chuàng)建本地密鑰
        KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
        keyAgree.init(priKey);
        keyAgree.doPhase(pubKey, true);
        // 創(chuàng)建了一個(gè)本地密鑰
        SecretKey secretKey = keyAgree.generateSecret(SECRET_ALGORITH);
        return secretKey.getEncoded();
    }

AB系統(tǒng)加解密數(shù)據(jù)

使用構(gòu)建的本地密鑰對(duì)進(jìn)行數(shù)據(jù)加解密:

/**
     * 使用本地密鑰加密數(shù)據(jù)
     * 
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
        // 構(gòu)建本地密鑰
        SecretKey secretKey = new SecretKeySpec(key, SECRET_ALGORITH);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    /**
     * 使用本地密鑰解密數(shù)據(jù)
     * 
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] key, byte[] data) throws Exception {
        // 構(gòu)建本地密鑰
        SecretKey secretKey = new SecretKeySpec(key, SECRET_ALGORITH);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

完整的代碼流程

NOTE:下面的代碼最后加解密步驟無法完成,之前提過,對(duì)稱加密技術(shù),JAVA的AES僅支持128位長(zhǎng)度的密鑰,DH最少產(chǎn)生512的密鑰,所以一般Java系統(tǒng)無法支持,需要下載授權(quán)文件。

public class DHTest {
    // 密鑰交換算法
    public static final String KEY_ALGORITH = "DH";
    // 對(duì)稱加密算法
    public static final String SECRET_ALGORITH = "AES";
    // DH算法的密鑰長(zhǎng)度
    private static final int KEY_SIZE = 512;
    // Map的一些參數(shù)
    private static final String MAP_KEY_PUBLIC = "DHPublicKey";
    private static final String MAP_KEY_PRIVATGE = "DHPrivateKey";

    public static void main(String[] args) throws Exception {
        // A系統(tǒng)構(gòu)建自己的公私鑰
        Map<String, Key> aKeyMap = initASysKey();
        byte[] aPubKey = aKeyMap.get(MAP_KEY_PUBLIC).getEncoded();
        byte[] aPriKey = aKeyMap.get(MAP_KEY_PRIVATGE).getEncoded();
        logKeyMap("A", aPubKey, aPriKey);

        // A將自己的公鑰發(fā)給B,B構(gòu)建自己的公私鑰
        // 一般都是發(fā)送二進(jìn)制或者base64等數(shù)據(jù)
        Map<String, Key> bKeyMap = initBSysKey(aPubKey);
        byte[] bPubKey = bKeyMap.get(MAP_KEY_PUBLIC).getEncoded();
        byte[] bPriKey = bKeyMap.get(MAP_KEY_PRIVATGE).getEncoded();
        logKeyMap("B", bPubKey, bPriKey);

        // A B系統(tǒng)產(chǎn)生自己的本地對(duì)稱加密算法密鑰
        byte[] aSecretKey = getSecretKey(bPubKey, aPriKey);
        byte[] bSecretKey = getSecretKey(aPubKey, bPriKey);

        // 轉(zhuǎn)換為字符串比較下
        String aSecKeyStr = toBase64(aSecretKey);
        String bSecKeyStr = toBase64(bSecretKey);
        log("A SecretKey : %s", aSecKeyStr);
        log("B SecretKey : %s", bSecKeyStr);
        log("A B SecretKey equeals : %s", aSecKeyStr.equals(bSecKeyStr));

        log("%s", aSecretKey.length);

        // A加密數(shù)據(jù),B解密數(shù)據(jù),能正常加解密
        String input = "A要發(fā)送給B的數(shù)據(jù)";
        byte[] data = encrypt(aSecretKey, input.getBytes());
        byte[] rs = decrypt(bSecretKey, data);
        log("A Send : %s , B Recive : %s ", toBase64(data), new String(rs));
    }

    /***
     * A系統(tǒng)產(chǎn)生密鑰,這個(gè)過程不需要參數(shù),由DH算法計(jì)算得出<br/>
     * 內(nèi)部使用一些安全的隨機(jī)函數(shù)隨機(jī)計(jì)算出一個(gè)公私鑰<br>
     * 計(jì)算后的公私鑰要存儲(chǔ)下來,存儲(chǔ)二進(jìn)制數(shù)據(jù)
     * 
     * @return
     * @throws Exception
     */
    public static Map<String, Key> initASysKey() throws Exception {
        // 使用DH算法生成公司密鑰
        KeyPairGenerator keyPairGr = KeyPairGenerator.getInstance(KEY_ALGORITH);
        keyPairGr.initialize(KEY_SIZE);
        KeyPair keyPair = keyPairGr.generateKeyPair();

        // 可以使用DB算法專業(yè)密鑰前行轉(zhuǎn)換獲取的PublicKey和PrivateKey
        // DHPublicKey dhPK = (DHPublicKey) publicKey;
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // 存儲(chǔ),傳輸密鑰
        Map<String, Key> keyMap = new HashMap<String, Key>(2);
        keyMap.put(MAP_KEY_PUBLIC, publicKey);
        keyMap.put(MAP_KEY_PRIVATGE, privateKey);
        return keyMap;
    }

    /**
     * B構(gòu)建自己的公私鑰,要使用A的公鑰構(gòu)建<br>
     * DH算法接受公鑰,并構(gòu)建自己的密鑰對(duì)<br>
     * 這個(gè)過程中用到了一些密鑰格式轉(zhuǎn)換對(duì)象,不是重點(diǎn)<br>
     * B也要保持自己的密鑰對(duì),二進(jìn)制形式
     * 
     * @param pubKey
     * @return
     * @throws Exception
     */
    public static Map<String, Key> initBSysKey(byte[] pubKey) throws Exception {

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITH);
        // 密鑰格式轉(zhuǎn)換對(duì)象
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
        // 轉(zhuǎn)換PublicKey格式
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
        // 構(gòu)建DH算法參數(shù)
        DHParameterSpec dhParamSpec = ((DHPublicKey) publicKey).getParams();
        // 使用DH算法創(chuàng)建密鑰對(duì)
        KeyPairGenerator keyPairGr = KeyPairGenerator.getInstance(keyFactory.getAlgorithm());
        keyPairGr.initialize(dhParamSpec);
        KeyPair keyPair = keyPairGr.generateKeyPair();
        // 創(chuàng)建的公私鑰
        DHPublicKey publicKey1 = (DHPublicKey) keyPair.getPublic();
        DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
        // 存儲(chǔ)傳輸密鑰
        Map<String, Key> keyMap = new HashMap<String, Key>(2);
        keyMap.put(MAP_KEY_PUBLIC, publicKey1);
        keyMap.put(MAP_KEY_PRIVATGE, privateKey);
        return keyMap;
    }

    /**
     * 使用對(duì)方的公鑰和自己的私鑰構(gòu)建對(duì)稱加密的SecretKey<br>
     * 
     * @param publicKey
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
        // 建立密鑰工廠
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITH);
        // 密鑰編碼轉(zhuǎn)換對(duì)象
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
        // 轉(zhuǎn)換公鑰格式
        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
        // 密鑰編碼轉(zhuǎn)換對(duì)象
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
        // 轉(zhuǎn)換私鑰格式
        PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
        // 利用公鑰和私鑰創(chuàng)建本地密鑰
        KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
        keyAgree.init(priKey);
        keyAgree.doPhase(pubKey, true);
        // 創(chuàng)建了一個(gè)本地密鑰
        SecretKey secretKey = keyAgree.generateSecret(SECRET_ALGORITH);
        return secretKey.getEncoded();
    }

    /**
     * 使用本地密鑰加密數(shù)據(jù)
     * 
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
        // 構(gòu)建本地密鑰
        SecretKey secretKey = new SecretKeySpec(key, SECRET_ALGORITH);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    /**
     * 使用本地密鑰解密數(shù)據(jù)
     * 
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] key, byte[] data) throws Exception {
        // 構(gòu)建本地密鑰
        SecretKey secretKey = new SecretKeySpec(key, SECRET_ALGORITH);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    public static void log(String tmp, Object... params) {
        System.out.println(String.format(tmp, params));
    }

    public static void logKeyMap(String sysName, byte[] pubKey, byte[] priKey) {
        log("%s Public Key : %s", sysName, toBase64(pubKey));
        log("%s Private Key : %s", sysName, toBase64(priKey));
    }

    private static String toBase64(byte[] data) {
        return new String(Base64.getEncoder().encode(data));
    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文中首先解釋了加密解密的一些基礎(chǔ)知識(shí)和概念,然后通過一個(gè)加密通信過程的例子說明了加密算法的作用,以及數(shù)字證書的出現(xiàn)...
    納蘭三少閱讀 2,042評(píng)論 1 6
  • 專題一,主要介紹HTTPS建立安全鏈接的原理,包括非對(duì)稱加密、對(duì)稱加密、CA認(rèn)證等知識(shí),還包括對(duì)一些業(yè)界常用算法的...
    有何不可12317閱讀 356評(píng)論 0 0
  • 文中首先解釋了加密解密的一些基礎(chǔ)知識(shí)和概念,然后通過一個(gè)加密通信過程的例子說明了加密算法的作用,以及數(shù)字證書的出現(xiàn)...
    sunny沖哥閱讀 1,489評(píng)論 0 3
  • 簡(jiǎn)介 在早期,互聯(lián)網(wǎng)通信是直接以明文進(jìn)行傳輸,幾乎沒任何安全性可言,在你發(fā)送數(shù)據(jù)到接收者手上,經(jīng)過n個(gè)網(wǎng)絡(luò)設(shè)備,在...
    li_zw閱讀 4,361評(píng)論 0 4
  • 7:45我們從榆中一中出發(fā)前往學(xué)農(nóng)地點(diǎn)。經(jīng)歷了一路的顛簸,11點(diǎn)我們終于平安到達(dá)。在當(dāng)?shù)剞r(nóng)戶的帶領(lǐng)下,我...
    習(xí)習(xí)晨風(fēng)閱讀 265評(píng)論 0 0

友情鏈接更多精彩內(nèi)容