服务器之家

服务器之家 > 正文

java 中DH的方式实现非对称加密的实例

时间:2020-12-11 15:18     来源/作者:Sahadev_

javaDH的方式实现非对称加密的实例

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.sahadev;
 
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;
 
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
 
import org.apache.commons.codec.binary.Hex;
 
public class DH {
 
  public static String src = "Hello,sahadev!";
 
  public static void main(String[] args) {
    JDKDH();
  }
 
  public static void JDKDH() {
    try {
      // 初始化发送方密钥
      KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance("DH");
      senderKeyPairGenerator.initialize(512);
      KeyPair senderKeyPair = senderKeyPairGenerator.generateKeyPair();
      byte[] senderPublicKey = senderKeyPair.getPublic().getEncoded();
 
      // 初始化接收方的密钥
      KeyFactory instance = KeyFactory.getInstance("DH");
      X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(senderPublicKey);
      PublicKey sendPublicKeyTemp = instance.generatePublic(x509EncodedKeySpec);
      DHParameterSpec dhParameterSpec = ((DHPublicKey) sendPublicKeyTemp).getParams();
      KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
      keyPairGenerator.initialize(dhParameterSpec);
      // 接收方生成密钥
      KeyPair generateKeyPair = keyPairGenerator.generateKeyPair();
      byte[] receiverPublicKey = generateKeyPair.getPublic().getEncoded();
      PrivateKey receiverPrivateKey = generateKeyPair.getPrivate();
 
      // 接收方密钥构建
      KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH");
      receiverKeyAgreement.init(receiverPrivateKey);
      receiverKeyAgreement.doPhase(senderKeyPair.getPublic(), true);
      // 使用我的密钥和你的公钥生成密钥
      SecretKey receiverDesKey = receiverKeyAgreement.generateSecret("DES");
 
      // 发送方密钥构建
      KeyFactory keyFactory = KeyFactory.getInstance("DH");
      X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(receiverPublicKey);
      PublicKey receiverPublicKeyTemp = keyFactory.generatePublic(x509EncodedKeySpec2);
 
      // 发送方拿到接收方返回的公钥做本地密钥
      KeyAgreement senderKeyAgreement = KeyAgreement.getInstance("DH");
      senderKeyAgreement.init(senderKeyPair.getPrivate());
      senderKeyAgreement.doPhase(receiverPublicKeyTemp, true);
      // 使用你的密钥我的公钥进行构建
      SecretKey senderDesKey = senderKeyAgreement.generateSecret("DES");
 
      // 判断双方的本地密钥是否相同
      if (Objects.equals(receiverDesKey, senderDesKey)) {
        System.out.println("双方密钥相同");
      }
 
      // 加密
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.ENCRYPT_MODE, senderDesKey);
      byte[] result = cipher.doFinal(src.getBytes());
      System.out.println("加密结果为 : " + Hex.encodeHexString(result));
 
      // 解密
      cipher.init(Cipher.DECRYPT_MODE, senderDesKey);
      result = cipher.doFinal(result);
      System.out.println("解密结果为 : " + new String(result));
    } catch (Exception e) {
      e.printStackTrace();
    }
 
  }
 
}

附上结果: 

?
1
2
3
双方密钥相同
DES : da1cc13d388423e82a110539414b1ec0
解密结果为 : Hello,sahadev!

以上就是DH实现非对称加密的实例,如有 疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/sahadev_/article/details/48773767

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部