Asymmetric Data Encryption DH algorithm and asymmetric Data Encryption dh Algorithm

Source: Internet
Author: User

Asymmetric Data Encryption DH algorithm and asymmetric Data Encryption dh Algorithm

Asymmetric encryption algorithms are relative to symmetric encryption algorithms. For more information about symmetric encryption algorithms, see the previous summary. Today we will introduce the DH algorithm. DH is a key exchange algorithm, the receiver generates the decryption key of the receiver based on the key encrypted by the sender. Let's take a look:

Initialize the sender key:

KeyPairGenerator sendKeyPairGenerator = KeyPairGenerator. getInstance ("DH"); sendKeyPairGenerator. initialize (512); KeyPair sendKeyPair = sendKeyPairGenerator. generateKeyPair (); byte [] sendPublicKeyEnc = sendKeyPair. getPublic (). getEncoded (); // generate the sender's public key and send it to the receiver (Network, file ...)

Initialize the receiver key:

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();

Build the receiver key:

KeyAgreement receiverKeyAgreement = KeyAgreement. getInstance ("DH"); receiverKeyAgreement. init (receiverPrivateKey); receiverKeyAgreement. doPhase (export epublickey, true); SecretKey receiverDESKey = receiverKeyAgreement. generateSecret ("DES"); // receiver Key

Build the sender 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 sendtransferey = sendKeyAgreement. generateSecret ("DES"); // The sender Key

Sender encryption:

Cipher sendCipher = Cipher.getInstance("DES");            sendCipher.init(Cipher.ENCRYPT_MODE, sendDESKey);            byte[] sendResult = sendCipher.doFinal(src.getBytes());            System.out.println("sendResult :"+Hex.encodeHexString(sendResult));

Receiver decryption:

Cipher receiverCipher = Cipher.getInstance("DES");            receiverCipher.init(Cipher.DECRYPT_MODE, receiverDESKey);            byte[] receiverResult = receiverCipher.doFinal(sendResult);            System.out.println("receiverResult : "+new String (receiverResult));

The asymmetric encryption algorithm (DH) based on key exchange is summarized here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.