MD5与Android和PHP [英] md5 with Android and PHP

查看:273
本文介绍了MD5与Android和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是MD5,以确保我的帖子给backendserver运行PHP。 这些参数通过HTTP POST发送。

我有一个问题,我的MD5计算结果是在Android和PHP服务器不同,如果有一个在输入参数中的一个的U,A或ö。

在Android上,哈希通过该功能来计算:

 公共静态最终字符串的MD5(最终的String){
    尝试 {
        //创建MD5哈希
        消息摘要消化= java.security.MessageDigest中
                .getInstance(MD5);
        digest.update(s.getBytes());
        字节的报文摘[] = digest.digest();

        //创建十六进制字符串
        StringBuffer的十六进制串=新的StringBuffer();
        的for(int i = 0; I< messageDigest.length;我++){
            串H = Integer.toHexString(0xFF的&放大器;报文摘[I]);
            而(h.length()2)
                H =0+ H;
            hexString.append(H);
        }
        返回hexString.toString();

    }赶上(抛出:NoSuchAlgorithmException E){
        e.printStackTrace();
    }
    返回 ;
}
 

PHP的服务器上,我只是用

  MD5()函数。
 

解决方案

看起来你需要传递的 UTF-8 连接codeD字符串 MD5 在PHP中:

  MD5(utf8_en code($字符串));
 

I am using an md5 to secure my posts to a backendserver running PHP. The parameters are send via HTTP Post.

I have one problem, the result of my md5 calculation is different on Android and the PHP server if there is a ü, ä or ö in one of the input parameters.

On Android, the hash is calculated via this function:

public static final String md5(final 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 "";
}

on the PHP server I simply use

md5() function.

解决方案

Looks like you need to pass utf-8 encoded string to md5 in PHP:

md5(utf8_encode($string));

这篇关于MD5与Android和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆