【问题标题】:How to do MD5 with foreign characters?外来字符怎么做MD5?
【发布时间】:2015-06-23 01:24:22
【问题描述】:

所以我正在使用统一引擎在手机游戏中制作我的自定义高分板。

我设置了我的 mysql 数据库,并从商店购买了高分资产,它可以工作,但只能使用英文用户名。所以基本上它将用户名,分数发送到 .php 脚本。

但我希望该脚本也可以接收韩语字符作为用户的昵称。我的用户也将使用韩文字符作为昵称,而不仅仅是英文字符。

我怎样才能做到这一点?

这里是代码。

------------------(统一端的 Highscore.cs)

WWWForm rsFm = new WWWForm();
            rsFm.AddField("name",name);   
        // at here name field, I want to receive korean characters as well.
            rsFm.AddField("tables",tables);
            rsFm.AddField("hash",GetHash(name));
            WWW rs = new WWW(registerUserURL,rsFm);
            yield return rs;

........

string GetHash(string usedString){ //Create a Hash to send to server
    MD5 md5 = MD5.Create();
    byte[] bytes = Encoding.ASCII.GetBytes(usedString+secretKey);
    byte[] hash = md5.ComputeHash(bytes);

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < hash.Length; i++){
        sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();

}

注册用户.php

<?php
include('ServerConnect.php');
$connection = Connect();//Attempt to Connect to MYSQL Server & DataBase

//Get variables from unity
$name = $_POST['name'];
$date = strtotime(date("Y-m-d"));
$tables = $_POST['tables'];
//Security Check
$hash = $_POST['hash'];
if(CheckKey($hash,$name) == 'fail'){ //Check if hash is valid
    echo 'Security Failure'; exit;

}

ServerConnect.php

function CheckKey($hash,$name){  //Check weather Md5 hash matches
    global $secretKey; 
    $checkHash = md5($name.$secretKey);
    if(strcmp($checkHash,$hash) == 0){
        return 'pass'; //hash matches
    }else{
        return 'fail'; //hash failed
    }

}

当我输入韩语字符并发送时,控制台结果在上面的代码中显示“安全失败”。

【问题讨论】:

  • 也许您应该在尝试散列之前对插入的值进行编码?
  • byte[] bytes = Encoding.ASCII.GetBytes(usedString+secretKey);。您的 C# 代码使用了错误的编码。
  • 也许您应该将分数或等价物与名称和盐一起散列,创建一个正确的校验和?如果您只对名称进行散列,用户可以嗅探名称/散列并使用不同的分数集发布。
  • @OfirBaruch 怎么办?
  • @Phylogenesis 那我应该怎么修改呢?

标签: php mysql unity3d md5


【解决方案1】:

就像用户之前所说的那样,如果您将使用 ascii 字符(韩语、日语等的情况),那么您使用的是错误的编码。您应该使用 Encoding.UTF8.GetBytes 而不是 Encoding.ASCII.GetBytes,请查看 https://msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.110%29.aspx 以获取示例函数 GetMd5Hash。如果你运行 ASCII md5,会生成一个不同的 md5。

“salt”是您使用的密钥。 PHP 中的 $secretKey 和 C# 中的 secretKey。如果您不知道什么是 salt,您应该阅读一些关于安全性的内容,因为如果您不知道,您会认为您创建了一个安全的 (r) 系统,而实际上您并没有。

希望对你有帮助。

【讨论】:

  • 谢谢!!!更改为 UTF8 即可解决。 'salt' 是我的自定义密钥,我设置了。
猜你喜欢
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
相关资源
最近更新 更多