【问题标题】:Android : How to create HMAC MD5 string? [closed]Android:如何创建 HMAC MD5 字符串? [关闭]
【发布时间】:2012-01-13 20:12:03
【问题描述】:

我正在尝试创建一个 android MD5 哈希字符串来等于下面的 C# 代码:

private string CalculateHMACMd5(string message, string key)
{
     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
     byte[] keyByte = encoding.GetBytes(key);
     HMACMD5 hmacmd5 = new HMACMD5(keyByte);
     byte[] messageBytes = encoding.GetBytes(message);
     byte[] hashmessage = hmacmd5.ComputeHash(messageBytes);
     string HMACMd5Value = ByteToString(hashmessage);
     return HMACMd5Value;
}

private static string ByteToString(byte[] buff)
{
    string sbinary = "";
    for (int i = 0; i < buff.Length; i++)
    {
        sbinary += buff[i].ToString("X2"); 
    }
    return (sbinary);
}


我目前使用的 Android 代码 [没有生成相同的 C# 代码]:
        public static String sStringToHMACMD5(String sData, String sKey) 
        {
            SecretKeySpec key;
            byte[] bytes;
            String sEncodedString = null;
            try 
            {       
                key = new SecretKeySpec((sKey).getBytes(), "ASCII");
                Mac mac = Mac.getInstance("HMACMD5");
                mac.init(key);
                mac.update(sData.getBytes());

                bytes = mac.doFinal(sData.getBytes());
                StringBuffer hash = new StringBuffer();

                for (int i=0; i<bytes.length; i++) {
                    String hex = Integer.toHexString(0xFF &  bytes[i]);
                    if (hex.length() == 1) {
                        hash.append('0');
                    }
                    hash.append(hex);
                }
            sEncodedString = hash.      
            return sEncodedString;
        }

提前致谢。

【问题讨论】:

  • @Thilo:我检查了你自己提供的链接,解决方案不起作用。
  • +您链接的答案不使用2个键(数据+键)

标签: java android hash md5 hmac


【解决方案1】:
public static String sStringToHMACMD5(String s, String keyString)
    {
        String sEncodedString = null;
        try
        {
            SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacMD5");
            Mac mac = Mac.getInstance("HmacMD5");
            mac.init(key);

            byte[] bytes = mac.doFinal(s.getBytes("ASCII"));

            StringBuffer hash = new StringBuffer();

            for (int i=0; i<bytes.length; i++) {
                String hex = Integer.toHexString(0xFF &  bytes[i]);
                if (hex.length() == 1) {
                    hash.append('0');
                }
                hash.append(hex);
            }
            sEncodedString = hash.toString();
        }
        catch (UnsupportedEncodingException e) {}
        catch(InvalidKeyException e){}
        catch (NoSuchAlgorithmException e) {}
        return sEncodedString ;
    }

【讨论】:

  • 不要忽略异常。记录它们。
  • 当然,但我删除了异常以减少上面的代码。
【解决方案2】:

定义“不工作”。例外?输出不符合预期?等

一个明显的事情是你处理相同的数据两次:

mac.update(sData.getBytes());
bytes = mac.doFinal(sData.getBytes());

要一次性处理所有数据,只需使用doFinal()(假设它不是太大)。 另一件可能出错的事情是密钥的格式:String sKey 的格式是什么。理想情况下,您应该使用 BASE64 编码字符串,而不是调用 getString()

【讨论】:

  • 我明白你的观点是正确的,请检查我的解决方案,谢谢提示。
  • 在两个程序中转储(或使用调试器)byte[] keyByte,确保字节相同。对byte[] messageBytes 执行相同操作。如果所有这些都匹配,byte[] hashmessage 也应该匹配,但也要检查一下。如果匹配,则错误在最终的十六进制编码部分。
猜你喜欢
  • 2016-07-05
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
相关资源
最近更新 更多