外来字符怎么做MD5?
How to do MD5 with foreign characters?
所以我正在使用 unity 引擎在手机游戏中制作自定义排行榜。
我设置了我的 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();
}
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;
}
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
}
}
当我输入韩文字符并发送时,控制台结果在上面的代码中显示 "Security Failure"。
就像用户之前所说的那样,如果您将使用 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。如果您不知道盐是什么,您应该阅读一些关于安全性的内容,因为如果您不知道,您会认为您创建了一个安全的 (r) 系统,而实际上您还没有。
希望对您有所帮助。
所以我正在使用 unity 引擎在手机游戏中制作自定义排行榜。
我设置了我的 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();
}
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;
}
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
}
}
当我输入韩文字符并发送时,控制台结果在上面的代码中显示 "Security Failure"。
就像用户之前所说的那样,如果您将使用 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。如果您不知道盐是什么,您应该阅读一些关于安全性的内容,因为如果您不知道,您会认为您创建了一个安全的 (r) 系统,而实际上您还没有。
希望对您有所帮助。