java中的非对称加密怎么利用DH方式实现-创新互联
本篇文章为大家展示了java 中的非对称加密怎么利用DH方式实现,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联是专业的思明网站建设公司,思明接单;提供网站设计制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行思明网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!实现代码:
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!
上述内容就是java 中的非对称加密怎么利用DH方式实现,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
文章题目:java中的非对称加密怎么利用DH方式实现-创新互联
转载源于:http://azwzsj.com/article/hhecg.html