Java加密技术(5)

Java加密技术(五)

   接下来我们分析DH加密算法,一种适基于密钥一致协议的加密算法。 
DH 
Diffie-Hellman算法(D-H算法),密钥一致协议。是由公开密钥密码*的奠基人Diffie和Hellman所提出的一种思想。简单的说就是允许两名用户在公开媒体上交换信息以生成"一致"的、可以共享的密钥。换句话说,就是由甲方产出一对密钥(公钥、私钥),乙方依照甲方公钥产生乙方密钥对(公钥、私钥)。以此为基线,作为数据传输保密基础,同时双方使用同一种对称加密算法构建本地密钥(SecretKey)对数据加密。这样,在互通了本地密钥(SecretKey)算法后,甲乙双方公开自己的公钥,使用对方的公钥和刚才产生的私钥加密数据,同时可以使用对方的公钥和自己的私钥对数据解密。不单单是甲乙双方两方,可以扩展为多方共享数据通讯,这样就完成了网络交互数据的安全通讯!该算法源于中国的同余定理——中国馀数定理。Java加密技术(5)  

流程分析: 

1.甲方构建密钥对儿,将公钥公布给乙方,将私钥保留;双方约定数据加密算法;乙方通过甲方公钥构建密钥对儿,将公钥公布给甲方,将私钥保留。 
2.甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给乙方加密后的数据;乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。 
3.乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给甲方加密后的数据;甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。 

    Java加密技术(5)
  1. Java加密技术(5)
  2. Java加密技术(5)

通过java代码实现如下:Coder类见 Java加密技术(一) 
Java代码  Java加密技术(5)
  1. import java.security.Key;  
  2. import java.security.KeyFactory;  
  3. import java.security.KeyPair;  
  4. import java.security.KeyPairGenerator;  
  5. import java.security.PublicKey;  
  6. import java.security.spec.PKCS8EncodedKeySpec;  
  7. import java.security.spec.X509EncodedKeySpec;  
  8. import java.util.HashMap;  
  9. import java.util.Map;  
  10.   
  11. import javax.crypto.Cipher;  
  12. import javax.crypto.KeyAgreement;  
  13. import javax.crypto.SecretKey;  
  14. import javax.crypto.interfaces.DHPrivateKey;  
  15. import javax.crypto.interfaces.DHPublicKey;  
  16. import javax.crypto.spec.DHParameterSpec;  
  17.   
  18. /** 
  19.  * DH安全编码组件 
  20.  *  
  21.  * @author 梁栋 
  22.  * @version 1.0 
  23.  * @since 1.0 
  24.  */  
  25. public abstract class DHCoder extends Coder {  
  26.     public static final String ALGORITHM = "DH";  
  27.   
  28.     /** 
  29.      * 默认密钥字节数 
  30.      *  
  31.      * <pre> 
  32.      * DH 
  33.      * Default Keysize 1024   
  34.      * Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive). 
  35.      * </pre> 
  36.      */  
  37.     private static final int KEY_SIZE = 1024;  
  38.   
  39.     /** 
  40.      * DH加密下需要一种对称加密算法对数据加密,这里我们使用DES,也可以使用其他对称加密算法。 
  41.      */  
  42.     public static final String SECRET_ALGORITHM = "DES";  
  43.     private static final String PUBLIC_KEY = "DHPublicKey";  
  44.     private static final String PRIVATE_KEY = "DHPrivateKey";  
  45.   
  46.     /** 
  47.      * 初始化甲方密钥 
  48.      *  
  49.      * @return 
  50.      * @throws Exception 
  51.      */  
  52.     public static Map<String, Object> initKey() throws Exception {  
  53.         KeyPairGenerator keyPairGenerator = KeyPairGenerator  
  54.                 .getInstance(ALGORITHM);  
  55.         keyPairGenerator.initialize(KEY_SIZE);  
  56.   
  57.         KeyPair keyPair = keyPairGenerator.generateKeyPair();  
  58.   
  59.         // 甲方公钥  
  60.         DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();  
  61.   
  62.         // 甲方私钥  
  63.         DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();  
  64.   
  65.         Map<String, Object> keyMap = new HashMap<String, Object>(2);  
  66.   
  67.         keyMap.put(PUBLIC_KEY, publicKey);  
  68.         keyMap.put(PRIVATE_KEY, privateKey);  
  69.         return keyMap;  
  70.     }  
  71.   
  72.     /** 
  73.      * 初始化乙方密钥 
  74.      *  
  75.      * @param key 
  76.      *            甲方公钥 
  77.      * @return 
  78.      * @throws Exception 
  79.      */  
  80.     public static Map<String, Object> initKey(String key) throws Exception {  
  81.         // 解析甲方公钥  
  82.         byte[] keyBytes = decryptBASE64(key);  
  83.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);  
  84.         KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);  
  85.         PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);  
  86.   
  87.         // 由甲方公钥构建乙方密钥  
  88.         DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams();  
  89.   
  90.         KeyPairGenerator keyPairGenerator = KeyPairGenerator  
  91.                 .getInstance(keyFactory.getAlgorithm());  
  92.         keyPairGenerator.initialize(dhParamSpec);  
  93.   
  94.         KeyPair keyPair = keyPairGenerator.generateKeyPair();  
  95.   
  96.         // 乙方公钥  
  97.         DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();  
  98.   
  99.         // 乙方私钥  
  100.         DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();  
  101.   
  102.         Map<String, Object> keyMap = new HashMap<String, Object>(2);  
  103.   
  104.         keyMap.put(PUBLIC_KEY, publicKey);  
  105.         keyMap.put(PRIVATE_KEY, privateKey);  
  106.   
  107.         return keyMap;  
  108.     }  
  109.   
  110.     /** 
  111.      * 加密<br> 
  112.      *  
  113.      * @param data 
  114.      *            待加密数据 
  115.      * @param publicKey 
  116.      *            甲方公钥 
  117.      * @param privateKey 
  118.      *            乙方私钥 
  119.      * @return 
  120.      * @throws Exception 
  121.      */  
  122.     public static byte[] encrypt(byte[] data, String publicKey,  
  123.             String privateKey) throws Exception {  
  124.   
  125.         // 生成本地密钥  
  126.         SecretKey secretKey = getSecretKey(publicKey, privateKey);  
  127.   
  128.         // 数据加密  
  129.         Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());  
  130.         cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  131.   
  132.         return cipher.doFinal(data);  
  133.     }  
  134.   
  135.     /** 
  136.      * 解密<br> 
  137.      *  
  138.      * @param data 
  139.      *            待解密数据 
  140.      * @param publicKey 
  141.      *            乙方公钥 
  142.      * @param privateKey 
  143.      *            乙方私钥 
  144.      * @return 
  145.      * @throws Exception 
  146.      */  
  147.     public static byte[] decrypt(byte[] data, String publicKey,  
  148.             String privateKey) throws Exception {  
  149.   
  150.         // 生成本地密钥  
  151.         SecretKey secretKey = getSecretKey(publicKey, privateKey);  
  152.         // 数据解密  
  153.         Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());  
  154.         cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  155.   
  156.         return cipher.doFinal(data);  
  157.     }  
  158.   
  159.     /** 
  160.      * 构建密钥 
  161.      *  
  162.      * @param publicKey 
  163.      *            公钥 
  164.      * @param privateKey 
  165.      *            私钥 
  166.      * @return 
  167.      * @throws Exception 
  168.      */  
  169.     private

文章评论