Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用bytearray中的密钥创建MAC_Java_Key_Hmac - Fatal编程技术网

Java 使用bytearray中的密钥创建MAC

Java 使用bytearray中的密钥创建MAC,java,key,hmac,Java,Key,Hmac,我有一个bytearray,想用它制作一个关键对象,它将被使用 初始化Mac对象。但是我不知道如何为这个制作一个关键对象,正确的关键类型等等。一些帮助将不胜感激 byte[] key2 = rsaDec.doFinal(encKey2); //assume this is correct Mac mac = Mac.getInstance("HmacMD5"); Key macKey = new Key //heres the issue at hand mac.init(macKey); b

我有一个bytearray,想用它制作一个关键对象,它将被使用 初始化Mac对象。但是我不知道如何为这个制作一个关键对象,正确的关键类型等等。一些帮助将不胜感激

byte[] key2 = rsaDec.doFinal(encKey2); //assume this is correct
Mac mac = Mac.getInstance("HmacMD5");

Key macKey = new Key //heres the issue at hand
mac.init(macKey);
byte[] message = ... //this will be retrieved
mac.update(message);
byte[] macVal = mac.doFinal();

谢谢

一种方法是:

 String keyString = "theKeyImUsing";
 SecretKeySpec macKey = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacMD5");
 mac.init(macKey);
如果您已经有字节[],则只需传递它:

     SecretKeySpec macKey = new SecretKeySpec(myByteArray, "HmacMD5");

Thx很多,我会试试这个!