欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-19 22:21:10
java 中dh的方式实现非对称加密的实例 实现代码: package com.sahadev; import java.security.keyfa...

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

实现代码:

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

附上结果: 

双方密钥相同 
des : da1cc13d388423e82a110539414b1ec0 
解密结果为 : hello,sahadev! 

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