您的位置:首页技术文章
文章详情页

JAVA加密算法实密钥一致协议代码示例

【字号: 日期:2022-08-22 13:47:58浏览:3作者:猪猪

密钥一致协议是由公开密钥密码体制的奠基人 Diffie 和 Hellman 所提出的一种思想。

代表:指数密钥一致协议 (Exponential Key Agreement Protocol)

使用流程介绍:

甲方构建密钥对,将公钥公布给乙方,将私钥保留;双方约定数据加密算法;乙方通过甲方公钥构建密钥对,将公钥公布给甲方,将私钥保留。

甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给乙方加密后的数据;乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。

乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给甲方加密后的数据;甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。

不单单是甲乙双方两方,可以扩展为多方共享数据通讯,这样就完成了网络交互数据的安全通讯!

参考示例:

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 构建密钥对,公钥给BSecurity.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 的公钥 相同');elseSystem.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)));}}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Java
相关文章: