Mac: init(Key key) throws InvalidKeyException : Mac « javax.crypto « Java by API






Mac: init(Key key) throws InvalidKeyException

  
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class MainClass {
  public static void main(String args[]) throws Exception {
    SecretKeySpec k = new SecretKeySpec("1234".getBytes(), "HMACSHA1");
    Mac m = Mac.getInstance("HmacMD5");
    m.init(k);
    m.update("test".getBytes("UTF8"));
    byte s[] = m.doFinal();
    for (int i = 0; i < s.length; i++) {
      System.out.print( Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
    }
  }
}

   
    
  








Related examples in the same category

1.Mac: getInstance(String algorithm) throws NoSuchAlgorithmException
2.Mac: update(byte[] input) throws IllegalStateException