Skip to content

Instantly share code, notes, and snippets.

@perchits
Created April 23, 2021 08:31
Show Gist options
  • Save perchits/fb5a9c6f670dfd9caaa408db13ea21e7 to your computer and use it in GitHub Desktop.
Save perchits/fb5a9c6f670dfd9caaa408db13ea21e7 to your computer and use it in GitHub Desktop.
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import java.security.Security;
public class MacExample {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String message = args[0];
byte[] secretSymmetricKey = Hex.decodeHex("20f7e4926a4b66c8a32071ff56aedc82".toCharArray());
Mac aescmac = Mac.getInstance("AESCMAC", "BC");
SecretKeySpec msgSpec = new SecretKeySpec(secretSymmetricKey, "AES");
aescmac.init(msgSpec);
aescmac.update(message.getBytes());
byte[] mac = aescmac.doFinal();
System.out.println(Hex.encodeHex(mac));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment