encrypt HMAC - Java Security

Java examples for Security:HMAC

Description

encrypt HMAC

Demo Code


//package com.java2s;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;

public class Main {

    public static final String KEY_MAC = "HmacMD5";

    public static byte[] encryptHMAC(byte[] data, String key)
            throws Exception {

        SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);/* ww w.ja v a  2  s  .  c  o  m*/

        return mac.doFinal(data);

    }

    public static byte[] decryptBASE64(String key) throws Exception {
        return (new BASE64Decoder()).decodeBuffer(key);
    }
}

Related Tutorials