Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of the implementation and Application of Java message Digest algorithm MAC

Shulou Source: shulou.com Published: 2022-06-02 04:38:21 10月03日 Update

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!

Tags: Summary algorithm application key message example analysis content instance method more problem helpless for this code function reason parameter this skill Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Linux Huawei NVidia