JAVA加密解密之DH算法

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;
   }
}

你可能感兴趣的:(java,加密,解密,DH)