`
cuisuqiang
  • 浏览: 3937238 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
3feb66c0-2fb6-35ff-968a-5f5ec10ada43
Java研发技术指南
浏览量:3651644
社区版块
存档分类
最新评论

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

    博客分类:
  • JDK
阅读更多

密钥一致协议是由公开密钥密码体制的奠基人 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 构建密钥对,公钥给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)));
	}
}

 

请您到ITEYE网站看 java小强 原创,谢谢!

http://cuisuqiang.iteye.com/ 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!

5
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics