In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Based on the example analysis of the implementation and application of Java message digest algorithm MAC, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The following example describes the implementation and application of Java message digest algorithm MAC. Share with you for your reference, the details are as follows:
An introduction
MAC:Message Authentication CodeHMAC:keyed-Hash Message Authencication Code, a hash function algorithm with keys.
Integration of MD and SHA
MD series: HmacMD2, HmacMD4, HmacMD5
SHA series: HmacSHA1, HmacSHA224, HmacSHA256, HmacSHA384, HmacSHA512
Application: SecureCRT
Two-parameter description
Three code implementation
Package com.imooc.security.hmac;import javax.crypto.KeyGenerator
Import javax.crypto.Mac
Import javax.crypto.SecretKey
Import javax.crypto.spec.SecretKeySpec
Import org.apache.commons.codec.binary.Hex
Import org.bouncycastle.crypto.digests.MD5Digest
Import org.bouncycastle.crypto.macs.HMac
Import org.bouncycastle.crypto.params.KeyParameter
Public class ImoocHmac {
Private static String src = "cakin24 security hmac"
Public static void main (String [] args) {jdkHmacMD5 ()
BcHmacMD5 ()
}
Public static void jdkHmacMD5 () {
Try {
KeyGenerator keyGenerator = KeyGenerator.getInstance ("HmacMD5")
/ / initialize KeyGenerator
SecretKey secretKey = keyGenerator.generateKey ()
/ / generate key / /
Byte [] key = secretKey.getEncoded ()
/ / obtain the key
Byte [] key = Hex.decodeHex (new char [] {'a','a', a'})
SecretKey restoreSecretKey = new SecretKeySpec (key, "HmacMD5")
/ / restore key
Mac mac = Mac.getInstance (restoreSecretKey.getAlgorithm ())
/ / instantiate MAC
Mac.init (restoreSecretKey)
/ / initialize Mac
Byte [] hmacMD5Bytes = mac.doFinal (src.getBytes ())
/ / Executive Summary
System.out.println ("jdk hmacMD5:" + Hex.encodeHexString (hmacMD5Bytes))
}
Catch (Exception e) {e.printStackTrace ()
}}
Public static void bcHmacMD5 () {
HMac hmac = new HMac (new MD5Digest ())
Hmac.init (new KeyParameter (org.bouncycastle.util.encoders.Hex.decode ("aaaaaaaaaa"); hmac.update (src.getBytes (), 0, src.getBytes () .length)
Byte [] hmacMD5Bytes = new byte [hmac.getMacSize ()]
/ / Executive Summary
Hmac.doFinal (hmacMD5Bytes, 0)
System.out.println ("bc hmacMD5:" + org.bouncycastle.util.encoders.Hex.toHexString (hmacMD5Bytes))
}}
Fourth, realize the effect
Jdk hmacMD5: d23aa029b2bacdd3979d10f0931f9af6bc hmacMD5: d23aa029b2bacdd3979d10f0931f9af6
After reading the above, have you mastered the method of example analysis of the implementation and application of the Java message digest algorithm MAC? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.