代码之家  ›  专栏  ›  技术社区  ›  ayush

hmac消息加密,但使用我们自己的密钥

  •  1
  • ayush  · 技术社区  · 14 年前
    try { // Generate a key for the HMAC-MD5 keyed-hashing algorithm
         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) { }
    

    上面的代码将为我们提供使用HMAC对消息进行摘要的键。现在我想在java中实现一个HMAC逻辑,其中密钥由用户给定。 有什么想法吗?

    1 回复  |  直到 11 年前
        1
  •  1
  •   crazyscot    14 年前

    构造 KeySpec 使用提供的用户密钥(或 SecretKeySpec PBEKeySpec 如果是密码),则使用 SecretKeyFactory 把它变成一个 SecretKey .