Key Generator Mac
import java.security.Security; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; public class MainClass { public static void main(String args[]) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); String inputString = "www.java2s.com"; KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); SecretKey secretKey = keyGen.generateKey(); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); byte[] byteData = inputString.getBytes("UTF8"); byte[] macBytes = mac.doFinal(byteData); String macAsString = new sun.misc.BASE64Encoder().encode(macBytes); System.out.println("Authentication code is: " + macAsString); } }