Java小强个人技术博客站点    手机版
当前位置: 首页 >> 理论 >> JAVA加密算法实现用例 密钥一致协议

JAVA加密算法实现用例 密钥一致协议

80530 理论 | 2014-12-27

密钥一致协议是由公开密钥密码体制的奠基人 Diffie 和 Hellman 所提出的一种思想。
代表:指数密钥一致协议 (Exponential Key Agreement Protocol)

 

20100315_937490_image007_666347_30005_0.jpg

 

使用流程介绍:
甲方构建密钥对,将公钥公布给乙方,将私钥保留;双方约定数据加密算法;乙方通过甲方公钥构建密钥对,将公钥公布给甲方,将私钥保留。
甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给乙方加密后的数据;乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。
乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给甲方加密后的数据;甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。
不单单是甲乙双方两方,可以扩展为多方共享数据通讯,这样就完成了网络交互数据的安全通讯!

 

package test;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.Security;
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;
public class DHKey {
 public static void main(String argv[]) {
  try {
   DHKey my = new DHKey();
   my.run();
  } catch (Exception e) {
   System.err.println(e);
  }
 }
 private void run() throws Exception {
  // A 构建密钥对,公钥给B
  Security.addProvider(new com.sun.crypto.provider.SunJCE());
  KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
  aliceKpairGen.initialize(512);
  KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
  byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded(); // 公开密钥
  
  // B 根据A的公钥构建自己的密钥对,同时把自己生成的公钥给A,通过A的公钥和自己的私钥构建DES的密钥
  KeyFactory bobKeyFac = KeyFactory.getInstance("DH");
  X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(alicePubKeyEnc);
  PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec);
  DHParameterSpec dhParamSpec = ((DHPublicKey) alicePubKey).getParams();
  KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");
  bobKpairGen.initialize(dhParamSpec);
  KeyPair bobKpair = bobKpairGen.generateKeyPair();
  KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
  bobKeyAgree.init(bobKpair.getPrivate());
  bobKeyAgree.doPhase(alicePubKey, true);
  SecretKey bobDesKey = bobKeyAgree.generateSecret("DES");
  byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();
  
  // A 通过本地密钥和A的公钥构建DES密钥,这里还做一个验证
  KeyFactory aliceKeyFac = KeyFactory.getInstance("DH");
  x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc);
  PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec);
  KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
  aliceKeyAgree.init(aliceKpair.getPrivate()); // 秘密密钥
  aliceKeyAgree.doPhase(bobPubKey, true);
  SecretKey aliceDesKey = aliceKeyAgree.generateSecret("DES");
  if (aliceDesKey.equals(bobDesKey))
   System.out.println("A 和 B 的公钥 相同");
  else
   System.out.println("A 和 B 的公钥 不同");
  
  // B 通过密钥加密数据
  Cipher bobCipher = Cipher.getInstance("DES");
  bobCipher.init(Cipher.ENCRYPT_MODE, bobDesKey);
  String bobinfo = "这是B的机密信息";
  System.out.println("B 加密前原文 :" + bobinfo);
  byte[] cleartext = bobinfo.getBytes();
  byte[] ciphertext = bobCipher.doFinal(cleartext);
  
  // A 通过密钥解密数据
  Cipher aliceCipher = Cipher.getInstance("DES");
  aliceCipher.init(Cipher.DECRYPT_MODE, aliceDesKey);
  byte[] recovered = aliceCipher.doFinal(ciphertext);
  System.out.println("A解密 B 的信息 :" + (new String(recovered)));
 }
}

 

推荐您阅读更多有关于“ des 加密 密钥 公钥 一致性 ”的文章

上一篇:抢票,就应该这样 下一篇:模版换回,因不兼容

猜你喜欢

发表评论: