【问题标题】:Why MD5 Hash key is not matching with php generated MD5 Hash key?为什么 MD5 哈希键与 php 生成的 MD5 哈希键不匹配?
【发布时间】:2012-07-22 15:25:01
【问题描述】:

在我的 Android 应用程序中,我使用 MD5 哈希密钥来保证数据访问的安全性。我正在使用 Email+time+"postfix" 字符串来生成密钥。它运作良好。但问题是,当我使用至少有一个加号 (+) 的电子邮件地址(如a+b@gmail.com)时,服务器返回“访问密钥不正确”消息。我注意到它正在使用带有减号 (-) 、下划线 (_) 等符号的电子邮件地址,但它不适用于带有加号 (+) 的电子邮件地址。我正在使用这种方法生成 MD5 哈希键:

public static String MD5_Hash(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest
                .getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

【问题讨论】:

    标签: java android hash md5


    【解决方案1】:

    这就是我的做法:-

    public static String getMD5(String input) {
    
     byte[] source;
     try {
         source = input.getBytes("UTF-8");
     } catch (UnsupportedEncodingException e) {
         System.out.println("Error!");
     }
    
     String result = null;
     char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7',
             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
     try {
         MessageDigest md = MessageDigest.getInstance("MD5");
         md.update(source);
         byte temp[] = md.digest();
         char str[] = new char[16 * 2];
         int k = 0;
         for (int i = 0; i < 16; i++) {
             byte byte0 = temp[i];
             str[k++] = hexDigits[byte0 >>> 4 & 0xf];
             str[k++] = hexDigits[byte0 & 0xf];
         }
         result = new String(str);
     } catch (Exception e) {
     System.out.println("Error!");
    }
    return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 1970-01-01
      • 2020-12-08
      • 2019-10-10
      • 2011-09-12
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多