您好,登錄后才能下訂單哦!
本篇文章為大家展示了java 中的非對稱加密怎么利用DH方式實現,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
實現代碼:
package com.sahadev; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.X509EncodedKeySpec; import java.util.Objects; import javax.crypto.Cipher; import javax.crypto.KeyAgreement; import javax.crypto.SecretKey; import javax.crypto.interfaces.DHPublicKey; import javax.crypto.spec.DHParameterSpec; import org.apache.commons.codec.binary.Hex; public class DH { public static String src = "Hello,sahadev!"; public static void main(String[] args) { JDKDH(); } public static void JDKDH() { try { // 初始化發送方密鑰 KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance("DH"); senderKeyPairGenerator.initialize(512); KeyPair senderKeyPair = senderKeyPairGenerator.generateKeyPair(); byte[] senderPublicKey = senderKeyPair.getPublic().getEncoded(); // 初始化接收方的密鑰 KeyFactory instance = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(senderPublicKey); PublicKey sendPublicKeyTemp = instance.generatePublic(x509EncodedKeySpec); DHParameterSpec dhParameterSpec = ((DHPublicKey) sendPublicKeyTemp).getParams(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH"); keyPairGenerator.initialize(dhParameterSpec); // 接收方生成密鑰 KeyPair generateKeyPair = keyPairGenerator.generateKeyPair(); byte[] receiverPublicKey = generateKeyPair.getPublic().getEncoded(); PrivateKey receiverPrivateKey = generateKeyPair.getPrivate(); // 接收方密鑰構建 KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH"); receiverKeyAgreement.init(receiverPrivateKey); receiverKeyAgreement.doPhase(senderKeyPair.getPublic(), true); // 使用我的密鑰和你的公鑰生成密鑰 SecretKey receiverDesKey = receiverKeyAgreement.generateSecret("DES"); // 發送方密鑰構建 KeyFactory keyFactory = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(receiverPublicKey); PublicKey receiverPublicKeyTemp = keyFactory.generatePublic(x509EncodedKeySpec2); // 發送方拿到接收方返回的公鑰做本地密鑰 KeyAgreement senderKeyAgreement = KeyAgreement.getInstance("DH"); senderKeyAgreement.init(senderKeyPair.getPrivate()); senderKeyAgreement.doPhase(receiverPublicKeyTemp, true); // 使用你的密鑰我的公鑰進行構建 SecretKey senderDesKey = senderKeyAgreement.generateSecret("DES"); // 判斷雙方的本地密鑰是否相同 if (Objects.equals(receiverDesKey, senderDesKey)) { System.out.println("雙方密鑰相同"); } // 加密 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, senderDesKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("加密結果為 : " + Hex.encodeHexString(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE, senderDesKey); result = cipher.doFinal(result); System.out.println("解密結果為 : " + new String(result)); } catch (Exception e) { e.printStackTrace(); } } }
附上結果:
雙方密鑰相同 DES : da1cc13d388423e82a110539414b1ec0 解密結果為 : Hello,sahadev!
上述內容就是java 中的非對稱加密怎么利用DH方式實現,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。