JAVA加密解密之DH算法

本文介绍Diffie-Hellman密钥交换协议,一种确保共享密钥安全穿越不安全网络的方法。该文详细解释了其工作原理,并通过Java代码示例展示了如何实现密钥的生成、交换及加密解密过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Diffie-Hellman:一种确保共享KEY安全穿越不安全网络的方法,它是OAKLEY的一个组成部分。Whitfield Diffie与Martin Hellman在1976年提出了一个奇妙的密钥交换协议,称为Diffie-Hellman密钥交换协议/算法(Diffie-Hellman Key Exchange/Agreement Algorithm).这个机制的巧妙在于需要安全通信的双方可以用这个方法确定对称密钥。然后可以用这个密钥进行加密和解密。但是注意,这个密钥交换协议/算法只能用于密钥的交换,而不能进行消息的加密和解密。双方确定要用的密钥后,要使用其他对称密钥操作加密算法实际加密和解密消息。

import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPrivateKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;

/**
 * DH(Diffie-Hellman算法(D-H算法),密钥一致协议)
 * 
 * @author jianggujin
 * 
 */
public class DHCoder
{
   private final String ALGORITHM = "DH";

   /**
    * 默认密钥字节数
    * 
    * <pre>
    * 默认密钥大小为1024,密钥长度必须是64的倍数,从512到1024
    * </pre>
    */
   private final int KEY_SIZE = 1024;

   /** DH加密下需要一种对称加密算法对数据加密,这里我们使用DES,也可以使用其他对称加密算法。 **/
   public String SECRET_ALGORITHM = "DES";
   /** 公钥Key **/
   public static final String PUBLIC_KEY = "DHPublicKey";
   /** 私钥Key **/
   public static final String PRIVATE_KEY = "DHPrivateKey";

   /**
    * 初始化甲方密钥
    * 
    * @return
    * @throws Exception
    */
   public Map<String, byte[]> initKey() throws Exception
   {
      KeyPairGenerator keyPairGenerator = KeyPairGenerator
            .getInstance(ALGORITHM);
      keyPairGenerator.initialize(KEY_SIZE);

      KeyPair keyPair = keyPairGenerator.generateKeyPair();

      // 甲方公钥
      DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();

      // 甲方私钥
      DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();

      Map<String, byte[]> keyMap = new HashMap<String, byte[]>(2);

      keyMap.put(PUBLIC_KEY, publicKey.getEncoded());
      keyMap.put(PRIVATE_KEY, privateKey.getEncoded());
      return keyMap;
   }

   /**
    * 初始化乙方密钥
    * 
    * @param key
    *           甲方公钥
    * @return
    * @throws Exception
    */
   public Map<String, byte[]> initKey(byte[] key) throws Exception
   {
      X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
      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();

      // 乙方公钥
      DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();

      // 乙方私钥
      DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();

      Map<String, byte[]> keyMap = new HashMap<String, byte[]>(2);

      keyMap.put(PUBLIC_KEY, publicKey.getEncoded());
      keyMap.put(PRIVATE_KEY, privateKey.getEncoded());

      return keyMap;
   }

   /**
    * 加密<br>
    * 
    * @param data
    *           待加密数据
    * @param publicKey
    *           甲方公钥
    * @param privateKey
    *           乙方私钥
    * @return
    * @throws Exception
    */
   public byte[] encrypt(byte[] data, byte[] publicKey, byte[] privateKey)
         throws Exception
   {
      // 生成本地密钥
      SecretKey secretKey = getSecretKey(publicKey, privateKey);

      // 数据加密
      Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
      cipher.init(Cipher.ENCRYPT_MODE, secretKey);

      return cipher.doFinal(data);
   }

   /**
    * 解密<br>
    * 
    * @param data
    *           待解密数据
    * @param publicKey
    *           乙方公钥
    * @param privateKey
    *           乙方私钥
    * @return
    * @throws Exception
    */
   public byte[] decrypt(byte[] data, byte[] publicKey, byte[] privateKey)
         throws Exception
   {
      // 生成本地密钥
      SecretKey secretKey = getSecretKey(publicKey, privateKey);
      // 数据解密
      Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
      cipher.init(Cipher.DECRYPT_MODE, secretKey);

      return cipher.doFinal(data);
   }

   /**
    * 构建密钥
    * 
    * @param publicKey
    *           公钥
    * @param privateKey
    *           私钥
    * @return
    * @throws Exception
    */
   private SecretKey getSecretKey(byte[] publicKey, byte[] privateKey)
         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(SECRET_ALGORITHM);

      return secretKey;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值