情報暗号化の非対称暗号化DHアルゴリズム
4392 ワード
非対称暗号化アルゴリズムは対称暗号化アルゴリズムに対して、対称暗号化アルゴリズムについて前のまとめを参照してください.今日はDHアルゴリズムを紹介します.DHは鍵交換アルゴリズムで、受信者は送信者が暗号化したときの鍵に基づいて、受信者の秘密鍵を生成します.一緒に勉強しましょう.
送信者キーの初期化:
受信者キーの初期化:
受信者キーの構築:
送信者キーの構築:
送信者の暗号化:
受信者の復号化:
鍵交換に基づく非対称暗号化アルゴリズム(DH)については,ここまでにまとめる.
送信者キーの初期化:
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));
鍵交換に基づく非対称暗号化アルゴリズム(DH)については,ここまでにまとめる.