zoukankan      html  css  js  c++  java
  • 信息加密之非对称加密DH算法

      非对称加密算法是相对于对称加密算法来说的,对于对称加密算法请查阅之前的总结,今天为大家介绍一下DH算法,DH是一种密钥交换算法,接收方根据发送方加密时的密钥,生成接收方解密密钥。下面就一起来学习一下吧:

    初始化发送方密钥:

    KeyPairGenerator sendKeyPairGenerator = KeyPairGenerator.getInstance("DH");
                sendKeyPairGenerator.initialize(512);
                KeyPair sendKeyPair = sendKeyPairGenerator.generateKeyPair();
                byte[] sendPublicKeyEnc = sendKeyPair.getPublic().getEncoded();//生成发送方公钥,发送给接收方(网络、文件...)

    初始化接收方密钥:

    KeyFactory receiverKeyFactory = KeyFactory.getInstance("DH");
                X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(sendPublicKeyEnc);
                PublicKey receivePublicKey = receiverKeyFactory.generatePublic(x509EncodedKeySpec);
                DHParameterSpec dhParameterSpec = ((DHPublicKey)receivePublicKey).getParams();
                KeyPairGenerator receiverKeyPairGenerator = KeyPairGenerator.getInstance("DH");
                receiverKeyPairGenerator.initialize(dhParameterSpec);
                KeyPair receiverKeyPair = receiverKeyPairGenerator.generateKeyPair();
                PrivateKey receiverPrivateKey = receiverKeyPair.getPrivate();
                byte[] receiverPublicKeyEnc = receiverKeyPair.getPublic().getEncoded();

    构建接收方密钥:

    KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH");
                receiverKeyAgreement.init(receiverPrivateKey);
                receiverKeyAgreement.doPhase(receivePublicKey, true);
                
                SecretKey receiverDESKey = receiverKeyAgreement.generateSecret("DES");//接收方Key

    构建发送方密钥:

    KeyFactory sendKeyFactory = KeyFactory.getInstance("DH");
                x509EncodedKeySpec = new X509EncodedKeySpec(receiverPublicKeyEnc);
                PublicKey sendPublicKey = sendKeyFactory.generatePublic(x509EncodedKeySpec);
                
                KeyAgreement sendKeyAgreement = KeyAgreement.getInstance("DH");
                sendKeyAgreement.init(sendKeyPair.getPrivate());
                sendKeyAgreement.doPhase(sendPublicKey, true);
                
                SecretKey sendDESKey = sendKeyAgreement.generateSecret("DES");//发送方Key

    发送方加密:

    Cipher sendCipher = Cipher.getInstance("DES");
                sendCipher.init(Cipher.ENCRYPT_MODE, sendDESKey);
                byte[] sendResult = sendCipher.doFinal(src.getBytes());
                System.out.println("sendResult :"+Hex.encodeHexString(sendResult));

    接收方解密:

    Cipher receiverCipher = Cipher.getInstance("DES");
                receiverCipher.init(Cipher.DECRYPT_MODE, receiverDESKey);
                byte[] receiverResult = receiverCipher.doFinal(sendResult);
                System.out.println("receiverResult : "+new String (receiverResult));

      基于密钥交换的非对称加密算法(DH),到这里就总结完毕了。

  • 相关阅读:
    开采镍矿与冶炼加工镍的上市公司一览(转载)
    2010年螺纹期货基本走势分析(个人分析原创文章)
    从松柏那转载的ajax类
    推荐30个可以养站的博客(转载)
    年报掘金:机构增仓路线图曝光(20100306转载)
    一个人过习惯了,两个人不知道怎么过。。。
    各大搜索引擎的网站登录入口(转载)
    志向
    主力做庄骗术
    网上发现襄阳市场,说是正品,初步推断举步维艰
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/4290634.html
Copyright © 2011-2022 走看看