Information Encryption message digest algorithm for Mac

Source: Internet
Author: User
Tags hmac

Mac is the third way to implement the message digest algorithm, and the other two methods are: Md2\4\5, SHA.

JDK implementation for Mac: 1, default key mode

Private Static voidmac_jdk () {Try{keygenerator Keygenerator= Keygenerator.getinstance ("HmacMD5");//Initialize KeygeneratorSecretkey Secretkey = Keygenerator.generatekey ();//Generate key            byte[] key = secretkey.getencoded ();//get the default keySecretkey Restorsecretkey=NewSecretkeyspec (Key, "HmacMD5");//Restore KeyMac Mac = Mac.getinstance (Restorsecretkey.getalgorithm ());//An example of a MacMac.init (Restorsecretkey);//Initialize Mac            byte[] hmacmd5bytes = Mac.dofinal (Src.getbytes ());//Executive SummarySystem.out.println ("Hmacmd5byte:" +hex.encodehexstring (hmacmd5bytes)); } Catch(Exception e) {e.printstacktrace (); }        }

2. Dynamic key mode:

Private Static voidMac_jdk_dongtai () {Try {            byte[] key = Hex.decodehex (New Char[]{' A ', ' a ', ' a ', ' a ', ' a ', ' a ', ' a ', ' a ', ' a ', ' a '};//dynamically acquiring keysSecretkey Restorsecretkey=NewSecretkeyspec (Key, "HmacMD5");//Restore KeyMac Mac = Mac.getinstance (Restorsecretkey.getalgorithm ());//An example of a MacMac.init (Restorsecretkey);//Initialize Mac            byte[] hmacmd5bytes = Mac.dofinal (Src.getbytes ());//Executive SummarySystem.out.println ("Hmacmd5byte:" +hex.encodehexstring (hmacmd5bytes)); } Catch(Exception e) {e.printstacktrace (); }        }

BC Implementations of Mac:

Private Static voidMAC_BC () {HMAC HMAC=NewHMac (Newmd5digest ()); Hmac.init (NewKeyparameter (Org.bouncycastle.util.encoders.Hex.decode ("Aaaaaaaaaa"))); Hmac.update (Src.getbytes (),0, Src.getbytes (). length); byte[] Mac_bc_byte =New byte[Hmac.getmacsize ()];//Executive SummaryHmac.dofinal (mac_bc_byte, 0); System.out.println ("Mac_bc_byte:" +hex.encodehexstring (mac_bc_byte)); }

To today's Java Base64, symmetric encryption, message digest encryption Implementation Summary is completed, if any interested in this, but also hope to communicate. ([email protected])

Information Encryption message digest algorithm for Mac

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.