【问题标题】:create a MAC using key from bytearray使用 bytearray 中的密钥创建 MAC
【发布时间】:2014-09-16 16:58:03
【问题描述】:

我有一个字节数组,想用它制作一个 Key-objectct,它将被使用 初始化一个 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();

谢谢

【问题讨论】:

    标签: java key hmac


    【解决方案1】:

    一种方法可能是:

     String keyString = "theKeyImUsing";
     SecretKeySpec macKey = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacMD5");
     mac.init(macKey);
    

    如果您已经有了 byte[],那么只需传递它:

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

    【讨论】:

    • 非常感谢,我会试试这个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2022-12-04
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多