java密钥交换算法DH定义与应用实例分析
程序员文章站
2022-04-16 19:17:03
本文实例讲述了java密钥交换算法dh定义与应用。分享给大家供大家参考,具体如下:
一 对称加密缺点
密钥传递过程复杂,这是对称加密带来的困扰。
二 dh密钥交换算法...
本文实例讲述了java密钥交换算法dh定义与应用。分享给大家供大家参考,具体如下:
一 对称加密缺点
密钥传递过程复杂,这是对称加密带来的困扰。
二 dh密钥交换算法特点
构建本地密钥
双方密钥一致
三 dh相关参数
四 dh算法实现过程
1、初始化发送方的密钥(keypairgenerator、keypair、publickey)
2、初始化接受方的密钥(keyfactory、x509encodedkeyspec、dhpublickey、dhparameterspec、keypairgenerator、privatekey)
3、密钥构建(keyageement、secretkey、keyfactory、x509encodekeyspec、publickey)
4、加密和解密(cipher)
五 dh代码实现过程
package com.imooc.security.dh; 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 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.base64; //import com.sun.org.apache.xalan.internal.utils.objects; public class imoocdh { private static string src = "cakin24 security dh"; public static void main(string[] args) { jdkdh(); } public static void jdkdh() { try { //1.初始化发送方密钥 keypairgenerator senderkeypairgenerator = keypairgenerator.getinstance("dh"); senderkeypairgenerator.initialize(512); keypair senderkeypair = senderkeypairgenerator.generatekeypair(); byte[] senderpublickeyenc = senderkeypair.getpublic().getencoded();//发送方公钥,发送给接收方(网络、文件。。。) //2.初始化接收方密钥 keyfactory receiverkeyfactory = keyfactory.getinstance("dh"); x509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec(senderpublickeyenc); publickey receiverpublickey = receiverkeyfactory.generatepublic(x509encodedkeyspec); dhparameterspec dhparameterspec = ((dhpublickey)receiverpublickey).getparams(); keypairgenerator receiverkeypairgenerator = keypairgenerator.getinstance("dh"); receiverkeypairgenerator.initialize(dhparameterspec); keypair receiverkeypair = receiverkeypairgenerator.generatekeypair(); privatekey receiverprivatekey = receiverkeypair.getprivate(); byte[] receiverpublickeyenc = receiverkeypair.getpublic().getencoded(); //3.密钥构建 keyagreement receiverkeyagreement = keyagreement.getinstance("dh"); receiverkeyagreement.init(receiverprivatekey); receiverkeyagreement.dophase(receiverpublickey, true); secretkey receiverdeskey = receiverkeyagreement.generatesecret("des"); keyfactory senderkeyfactory = keyfactory.getinstance("dh"); x509encodedkeyspec = new x509encodedkeyspec(receiverpublickeyenc); publickey senderpublickey = senderkeyfactory.generatepublic(x509encodedkeyspec); keyagreement senderkeyagreement = keyagreement.getinstance("dh"); senderkeyagreement.init(senderkeypair.getprivate()); senderkeyagreement.dophase(senderpublickey, true); secretkey senderdeskey = senderkeyagreement.generatesecret("des"); //if (objects.equals(receiverdeskey, senderdeskey)) { if(receiverdeskey.equals(senderdeskey)){ system.out.println("双方密钥相同"); } //4.加密 cipher cipher = cipher.getinstance("des"); cipher.init(cipher.encrypt_mode, senderdeskey); byte[] result = cipher.dofinal(src.getbytes()); system.out.println("jdk dh encrypt : " + base64.encodebase64string(result)); //5.解密 cipher.init(cipher.decrypt_mode, receiverdeskey); result = cipher.dofinal(result); system.out.println("jdk dh decrypt : " + new string(result)); } catch (exception e) { e.printstacktrace(); } } }
六 实现效果
双方密钥相同
jdk dh encrypt : wqnkvpqve05ud64ocqgxcqu5rcssevsu
jdk dh decrypt : cakin24 security dh
七 应用场景
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含aes、des、rc4等):
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具: