`
wang吖
  • 浏览: 234626 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA加密算法实现用例 密钥一致协议(转载)

 
阅读更多

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

 

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

 

参考示例:

Java代码  收藏代码
  1. package test;  
  2. import java.security.KeyFactory;  
  3. import java.security.KeyPair;  
  4. import java.security.KeyPairGenerator;  
  5. import java.security.PublicKey;  
  6. import java.security.Security;  
  7. import java.security.spec.X509EncodedKeySpec;  
  8. import javax.crypto.Cipher;  
  9. import javax.crypto.KeyAgreement;  
  10. import javax.crypto.SecretKey;  
  11. import javax.crypto.interfaces.DHPublicKey;  
  12. import javax.crypto.spec.DHParameterSpec;  
  13. public class DHKey {  
  14.     public static void main(String argv[]) {  
  15.         try {  
  16.             DHKey my = new DHKey();  
  17.             my.run();  
  18.         } catch (Exception e) {  
  19.             System.err.println(e);  
  20.         }  
  21.     }  
  22.     private void run() throws Exception {  
  23.         // A 构建密钥对,公钥给B  
  24.         Security.addProvider(new com.sun.crypto.provider.SunJCE());  
  25.         KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");  
  26.         aliceKpairGen.initialize(512);  
  27.         KeyPair aliceKpair = aliceKpairGen.generateKeyPair();  
  28.         byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded(); // 公开密钥  
  29.           
  30.         // B 根据A的公钥构建自己的密钥对,同时把自己生成的公钥给A,通过A的公钥和自己的私钥构建DES的密钥  
  31.         KeyFactory bobKeyFac = KeyFactory.getInstance("DH");  
  32.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(alicePubKeyEnc);  
  33.         PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec);  
  34.         DHParameterSpec dhParamSpec = ((DHPublicKey) alicePubKey).getParams();  
  35.         KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");  
  36.         bobKpairGen.initialize(dhParamSpec);  
  37.         KeyPair bobKpair = bobKpairGen.generateKeyPair();  
  38.         KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");  
  39.         bobKeyAgree.init(bobKpair.getPrivate());  
  40.         bobKeyAgree.doPhase(alicePubKey, true);  
  41.         SecretKey bobDesKey = bobKeyAgree.generateSecret("DES");  
  42.         byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();  
  43.           
  44.         // A 通过本地密钥和A的公钥构建DES密钥,这里还做一个验证  
  45.         KeyFactory aliceKeyFac = KeyFactory.getInstance("DH");  
  46.         x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc);  
  47.         PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec);  
  48.         KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");  
  49.         aliceKeyAgree.init(aliceKpair.getPrivate()); // 秘密密钥  
  50.         aliceKeyAgree.doPhase(bobPubKey, true);  
  51.         SecretKey aliceDesKey = aliceKeyAgree.generateSecret("DES");  
  52.         if (aliceDesKey.equals(bobDesKey))  
  53.             System.out.println("A 和 B 的公钥 相同");  
  54.         else  
  55.             System.out.println("A 和 B 的公钥 不同");  
  56.           
  57.         // B 通过密钥加密数据  
  58.         Cipher bobCipher = Cipher.getInstance("DES");  
  59.         bobCipher.init(Cipher.ENCRYPT_MODE, bobDesKey);  
  60.         String bobinfo = "这是B的机密信息";  
  61.         System.out.println("B 加密前原文 :" + bobinfo);  
  62.         byte[] cleartext = bobinfo.getBytes();  
  63.         byte[] ciphertext = bobCipher.doFinal(cleartext);  
  64.           
  65.         // A 通过密钥解密数据  
  66.         Cipher aliceCipher = Cipher.getInstance("DES");  
  67.         aliceCipher.init(Cipher.DECRYPT_MODE, aliceDesKey);  
  68.         byte[] recovered = aliceCipher.doFinal(ciphertext);  
  69.         System.out.println("A解密 B 的信息 :" + (new String(recovered)));  
  70.     }  
  71. }  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics