新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
java中怎么DH非对称加密算法,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
十余年的滨海网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整滨海建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“滨海网站设计”,“滨海网站推广”以来,每个客户项目都认真落实执行。初始化发送方密钥:
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));
关于java中怎么DH非对称加密算法问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。