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

网友投稿 268 2023-04-23


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

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 senderKNjhCNKxyBeyPair = senderKeyPairGenerator.generateKeyPair();

byte[] senderPublicKey = senderKeyPair.getPublic().getEncoded();

// 初始化接收方的密钥

KeyFactory instance = KeyFactory.getInstance("DH");

X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKNjhCNKxyBeySpec(senderPublicKey);

PublicKey sendPublicKeyTemp = instance.generatePublic(x509EncodedKeySpec);

DHParameterSpec dhParameterSpec = ((DHPublicKey) sendPublicKeyTemp).getParams();

NjhCNKxyB 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实现非对称加密的实例,如有 疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:工厂模式_动力节点Java学院整理
下一篇:java序列化和serialVersionUID的使用方法实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~