织梦CMS - 轻松建站从此开始!

罗索

DH Diffie-Hellman算法(D-H算法),密钥一致协议。

落鹤生 发布于 2015-07-06 15:31 点击:次 
一个基于Java的DH算法的实现。
TAG: H323  DH算法  H235  

一个基于Java的DH算法的实现。

/**
 * DH Diffie-Hellman算法(D-H算法),密钥一致协议。<br>
 * 是由公开密钥密码体制的奠基人Diffie和Hellman所提出的一种思想。<br>
 * 简单的说就是允许两名用户在公开媒体上交换信息以生成"一致"的、可以共享的密钥。<br>
 * 换句话说,就是由甲方产出一对密钥(公钥、私钥),乙方依照甲方公钥产生乙方密钥对(公钥、私钥)。<br>
 * 以此为基线,作为数据传输保密基础,同时双方使用同一种对称加密算法构建本地密钥(SecretKey)对数据加密。<br>
 * 这样,在互通了本地密钥(SecretKey)算法后,甲乙双方公开自己的公钥,使用对方的公钥和刚才产生的私钥加密数据,同时可以使用对方的公钥和自己的私钥对数据解密。<br>
 * 不单单是甲乙双方两方,可以扩展为多方共享数据通讯,这样就完成了网络交互数据的安全通讯!该算法源于中国的同余定理——中国馀数定理。
 * <ul>
 * <li>甲方构建密钥对儿,将公钥公布给乙方,将私钥保留;双方约定数据加密算法;乙方通过甲方公钥构建密钥对儿,将公钥公布给甲方,将私钥保留。</li>
 * <li>甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给乙方加密后的数据;乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。</li>
 * <li>乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给甲方加密后的数据;甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。</li>
 * </ul>
 * 
 * @author Ice_Liu
 * 
 */

  1. package com.ice.webos.util.security; 
  2.  
  3. import java.security.Key; 
  4. import java.security.KeyFactory; 
  5. import java.security.KeyPair; 
  6. import java.security.KeyPairGenerator; 
  7. import java.security.PublicKey; 
  8. import java.security.spec.PKCS8EncodedKeySpec; 
  9. import java.security.spec.X509EncodedKeySpec; 
  10. import java.util.HashMap; 
  11. import java.util.Map; 
  12.  
  13. import javax.crypto.Cipher; 
  14. import javax.crypto.KeyAgreement; 
  15. import javax.crypto.SecretKey; 
  16. import javax.crypto.interfaces.DHPrivateKey; 
  17. import javax.crypto.interfaces.DHPublicKey; 
  18. import javax.crypto.spec.DHParameterSpec; 
  19.  
  20. public class DHCryptUtil { 
  21.     public static final String ALGORITHM = "DH"
  22.  
  23.     /** 
  24.      * 默认密钥字节数 
  25.      *  
  26.      * <pre> 
  27.      *   
  28.      * DH  
  29.      * Default Keysize 1024    
  30.      * Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive).  
  31.      * </pre> 
  32. */ 
  33.     private static final int KEY_SIZE = 1024; 
  34.  
  35.     /** 
  36.      * DH 加密下需要一种对称加密算法对数据加密,这里我们使用DES,也可以使用其他对称加密算法。 
  37. */ 
  38.     public static final String SECRET_ALGORITHM = "DES"
  39.     private static final String PUBLIC_KEY = "DHPublicKey"
  40.     private static final String PRIVATE_KEY = "DHPrivateKey"
  41.  
  42.     /** 
  43.      * 初始化甲方密钥 
  44.      *  
  45.      * @return 
  46.      * @throws Exception 
  47. */ 
  48.     public static Map<String, Object> initKey() throws Exception { 
  49.         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM); 
  50.         keyPairGenerator.initialize(KEY_SIZE); 
  51.  
  52.         KeyPair keyPair = keyPairGenerator.generateKeyPair(); 
  53.  
  54.         // 甲方公钥 
  55.         DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic(); 
  56.  
  57.         // 甲方私钥 
  58.         DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate(); 
  59.  
  60.         Map<String, Object> keyMap = new HashMap<String, Object>(2); 
  61.  
  62.         keyMap.put(PUBLIC_KEY, publicKey); 
  63.         keyMap.put(PRIVATE_KEY, privateKey); 
  64.         return keyMap; 
  65.     } 
  66.  
  67.     /** 
  68.      * 初始化乙方密钥 
  69.      *  
  70.      * @param key 
  71.      *            甲方公钥 
  72.      * @return 
  73.      * @throws Exception 
  74. */ 
  75.     public static Map<String, Object> initKey(String key) throws Exception { 
  76.         // 解析甲方公钥 
  77.         byte[] keyBytes = CryptUtil.decryptBASE64(key); 
  78.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); 
  79.         KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); 
  80.         PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); 
  81.  
  82.         // 由甲方公钥构建乙方密钥 
  83.         DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams(); 
  84.  
  85.         KeyPairGenerator keyPairGenerator
  86.  = KeyPairGenerator.getInstance(keyFactory.getAlgorithm()); 
  87.         keyPairGenerator.initialize(dhParamSpec); 
  88.  
  89.         KeyPair keyPair = keyPairGenerator.generateKeyPair(); 
  90.  
  91.         // 乙方公钥 
  92.         DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic(); 
  93.  
  94.         // 乙方私钥 
  95.         DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate(); 
  96.  
  97.         Map<String, Object> keyMap = new HashMap<String, Object>(2); 
  98.  
  99.         keyMap.put(PUBLIC_KEY, publicKey); 
  100.         keyMap.put(PRIVATE_KEY, privateKey); 
  101.  
  102.         return keyMap; 
  103.     } 
  104.  
  105.     /** 
  106.      * 加密<br> 
  107.      *  
  108.      * @param data 
  109.      *            待加密数据 
  110.      * @param publicKey 
  111.      *            甲方公钥 
  112.      * @param privateKey 
  113.      *            乙方私钥 
  114.      * @return 
  115.      * @throws Exception 
  116. */ 
  117.     public static byte[] encrypt(byte[] data, String publicKey, String privateKey)
  118.  throws Exception { 
  119.  
  120.         // 生成本地密钥 
  121.         SecretKey secretKey = getSecretKey(publicKey, privateKey); 
  122.  
  123.         // 数据加密 
  124.         Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); 
  125.         cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
  126.  
  127.         return cipher.doFinal(data); 
  128.     } 
  129.  
  130.     /** 
  131.      * 解密<br> 
  132.      *  
  133.      * @param data 
  134.      *            待解密数据 
  135.      * @param publicKey 
  136.      *            乙方公钥 
  137.      * @param privateKey 
  138.      *            乙方私钥 
  139.      * @return 
  140.      * @throws Exception 
  141. */ 
  142.     public static byte[] decrypt(byte[] data, String publicKey
  143. , String privateKey) throws Exception { 
  144.  
  145.         // 生成本地密钥 
  146.         SecretKey secretKey = getSecretKey(publicKey, privateKey); 
  147.         // 数据解密 
  148.         Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); 
  149.         cipher.init(Cipher.DECRYPT_MODE, secretKey); 
  150.  
  151.         return cipher.doFinal(data); 
  152.     } 
  153.  
  154.     /** 
  155.      * 构建密钥 
  156.      *  
  157.      * @param publicKey 
  158.      *            公钥 
  159.      * @param privateKey 
  160.      *            私钥 
  161.      * @return 
  162.      * @throws Exception 
  163. */ 
  164.     private static SecretKey getSecretKey(String publicKey
  165. , String privateKey) throws Exception { 
  166.         // 初始化公钥 
  167.         byte[] pubKeyBytes = CryptUtil.decryptBASE64(publicKey); 
  168.  
  169.         KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); 
  170.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyBytes); 
  171.         PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); 
  172.  
  173.         // 初始化私钥 
  174.         byte[] priKeyBytes = CryptUtil.decryptBASE64(privateKey); 
  175.  
  176.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKeyBytes); 
  177.         Key priKey = keyFactory.generatePrivate(pkcs8KeySpec); 
  178.  
  179.         KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm()); 
  180.         keyAgree.init(priKey); 
  181.         keyAgree.doPhase(pubKey, true); 
  182.  
  183.         // 生成本地密钥 
  184.         SecretKey secretKey = keyAgree.generateSecret(SECRET_ALGORITHM); 
  185.  
  186.         return secretKey; 
  187.     } 
  188.  
  189.     /** 
  190.      * 取得私钥 
  191.      *  
  192.      * @param keyMap 
  193.      * @return 
  194.      * @throws Exception 
  195. */ 
  196.     public static String getPrivateKey(Map<String, Object> keyMap)
  197.  throws Exception { 
  198.         Key key = (Key) keyMap.get(PRIVATE_KEY); 
  199.  
  200.         return CryptUtil.encryptBASE64(key.getEncoded()); 
  201.     } 
  202.  
  203.     /** 
  204.      * 取得公钥 
  205.      *  
  206.      * @param keyMap 
  207.      * @return 
  208.      * @throws Exception 
  209. */ 
  210.     public static String getPublicKey(Map<String, Object> keyMap)
  211.  throws Exception { 
  212.         Key key = (Key) keyMap.get(PUBLIC_KEY); 
  213.  
  214.         return CryptUtil.encryptBASE64(key.getEncoded()); 
  215.     } 
  216.  
  217.     public static void main(String[] args) { 
  218.         try { 
  219.             RSACryptUtil.main(args); 
  220.             System.out.println("**************************************"); 
  221.             System.out.println("DH加密算法"); 
  222.             // 生成甲方密钥对儿 
  223.             Map<String, Object> aKeyMap = DHCryptUtil.initKey(); 
  224.             String aPublicKey = DHCryptUtil.getPublicKey(aKeyMap); 
  225.             String aPrivateKey = DHCryptUtil.getPrivateKey(aKeyMap); 
  226.  
  227.             System.out.println("甲方公钥:\r" + aPublicKey); 
  228.             System.out.println("甲方私钥:\r" + aPrivateKey); 
  229.  
  230.             // 由甲方公钥产生乙方本地密钥对儿 
  231.             Map<String, Object> bKeyMap = DHCryptUtil.initKey(aPublicKey); 
  232.             String bPublicKey = DHCryptUtil.getPublicKey(bKeyMap); 
  233.             String bPrivateKey = DHCryptUtil.getPrivateKey(bKeyMap); 
  234.  
  235.             System.out.println("乙方公钥:\r" + bPublicKey); 
  236.             System.out.println("乙方私钥:\r" + bPrivateKey); 
  237.  
  238.             String aInput = "abc "
  239.             System.out.println("原文: " + aInput); 
  240.  
  241.             // 由甲方公钥,乙方私钥构建密文 
  242.             byte[] aCode = DHCryptUtil.encrypt(aInput.getBytes()
  243. , aPublicKey, bPrivateKey); 
  244.  
  245.             // 由乙方公钥,甲方私钥解密 
  246.             byte[] aDecode = DHCryptUtil.decrypt(aCode, bPublicKey, aPrivateKey); 
  247.             String aOutput = (new String(aDecode)); 
  248.  
  249.             System.out.println("解密: " + aOutput); 
  250.  
  251.             System.out.println(" ===============反过来加密解密================== "); 
  252.             String bInput = "def "
  253.             System.out.println("原文: " + bInput); 
  254.  
  255.             // 由乙方公钥,甲方私钥构建密文 
  256.             byte[] bCode = DHCryptUtil.encrypt(bInput.getBytes()
  257. , bPublicKey, aPrivateKey); 
  258.  
  259.             // 由甲方公钥,乙方私钥解密 
  260.             byte[] bDecode = DHCryptUtil.decrypt(bCode, aPublicKey, bPrivateKey); 
  261.             String bOutput = (new String(bDecode)); 
  262.  
  263.             System.out.println("解密: " + bOutput); 
  264.         } catch (Exception e) { 
  265.             // TODO Auto-generated catch block 
  266.             e.printStackTrace(); 
  267.         } 
  268.  
  269.     } 

 

(liubin0509)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201507/17347.html]
本文出处:博客园 作者:liubin0509 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容