The non-symmetric encryption DH algorithm for information encryption

Source: Internet
Author: User
Tags asymmetric encryption

Asymmetric encryption algorithm is relative to symmetric encryption algorithm, for symmetric encryption algorithm please refer to the previous summary, today for everyone to introduce the DH algorithm, DH is a key exchange algorithm, the receiver based on the sender encryption key, generate receiver decryption key. Let's take a look at the following:

Initialize Sender key:

Keypairgenerator sendkeypairgenerator = keypairgenerator.getinstance ("DH");            Sendkeypairgenerator.initialize (+);             = Sendkeypairgenerator.generatekeypair ();             byte [] Sendpublickeyenc = Sendkeypair.getpublic (). getencoded (); // generate sender Public key, send to receiver (network, file ...) )

Initialize the receiver key:

Keyfactory receiverkeyfactory = keyfactory.getinstance ("DH"); X509encodedkeyspec X509encodedkeyspec=NewX509encodedkeyspec (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 ();

To build the receiver key:

Keyagreement receiverkeyagreement = keyagreement.getinstance ("DH");            Receiverkeyagreement.init (Receiverprivatekey);             true );                         = Receiverkeyagreement.generatesecret ("DES"); // Receiving Party key

To build the sender key:

Keyfactory sendkeyfactory = keyfactory.getinstance ("DH");             New X509encodedkeyspec (RECEIVERPUBLICKEYENC);             = Sendkeyfactory.generatepublic (x509encodedkeyspec);                         = Keyagreement.getinstance ("DH");            Sendkeyagreement.init (Sendkeypair.getprivate ());             true );                         = Sendkeyagreement.generatesecret ("DES"); // Sender Key

Sender-Side 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 summed up here.

The non-symmetric encryption DH algorithm for information encryption

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.