Generating a Message Authentication Code (MAC) Key - Java Security

Java examples for Security:Key

Description

Generating a Message Authentication Code (MAC) Key

Demo Code

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Main {
  public static void main(String[] args) throws Exception {
    try {//from   ww  w  .j a v a 2 s. com
      KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
      SecretKey key = keyGen.generateKey();

      // Generate a key for the HMAC-SHA1 keyed-hashing algorithm
      keyGen = KeyGenerator.getInstance("HmacSHA1");
      key = keyGen.generateKey();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
  }
}

Related Tutorials