如何用外国字符做MD5?

cre*_*tor 9 php mysql md5 unity-game-engine

所以我在移动游戏中使用统一引擎制作我的自定义高分榜.

我设置了我的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;
Run Code Online (Sandbox Code Playgroud)

..................

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();
Run Code Online (Sandbox Code Playgroud)

}

RegisterUser.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;
Run Code Online (Sandbox Code Playgroud)

}

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
    }
Run Code Online (Sandbox Code Playgroud)

}

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

小智 4

就像用户之前所说的那样,如果您将使用超出 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。

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

希望能帮助到你。