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